Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
private void cboCustomer_KeyPress(object sender, KeyPressEventArgs e)
{
 //cboCustomer.Items.Clear();
 cboCustomer.DataBindings.Clear();
 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++)
   {                           
      //cboCustomer.Items.Clear();
      //cboCustomer.DataBindings.Clear();
      cboCustomer.Items.Add(table.Rows[i]["Company_Name"].ToString());
    }
  }
}

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

how should I change the program?

Thanks!!
Posted
Updated 9-Jan-13 13:12pm
v2

In VB.NET I use this:

VB
ddlTest.DataSource = testDataSource
ddlTest.DataTextField = "Name"
ddlTest.DataValueField = "ID"
ddlTest.DataBind()

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.
 
Share this answer
 
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?
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900