Click here to Skip to main content
12,815,986 members (40,149 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
How to show the data from mssql statement at ComboBox list after binding with datasource.

i write sql statement as

select customer.Company_Name from customer where customer.Company_Name like '%' + @status + '%'

and code is like that
private void cboCustomer_KeyPress(object sender, KeyPressEventArgs e)
 string str = cboCustomer.Text;
 DataSet data = logic.GetData_By_Desc("SearchbyCustomerName", str);
 DataTable table = data.Tables[0];
 if (table.Rows.Count > 0)
   for (int i = 0; i < table.Rows.Count; i++)

while using that, the error display as "Items collection cannot be modified when the DataSource property is set."

how should I change the program?

Posted 9-Jan-13 6:09am
Updated 9-Jan-13 14:12pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

In VB.NET I use this:

ddlTest.DataSource = testDataSource
ddlTest.DataTextField = "Name"
ddlTest.DataValueField = "ID"

ddlSocialMediaChannel.Items.Insert(0, "-- None --")
ddlSocialMediaChannel.Items(0).Value = ""

Should be the same in C#.

@Dave Kreskowiak - We use it to add a 'None' option to bound drop downs.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

OK, it's pretty clear. You can EITHER bind the combo to a datasource OR don't bind it and manually add the items to it. You can NOT do both.

The first question is what rea you trying to do with this code? WHY are you trying to add items to a bound combo?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170308.1 | Last Updated 9 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100