Click here to Skip to main content
11,428,221 members (61,942 online)
Rate this: bad
good
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)
{
 //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 9-Jan-13 6:09am
ttds828
Edited 9-Jan-13 14:12pm
Jibesh16.8K
v2
Rate this: bad
good
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"
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.
  Permalink  
Rate this: bad
good
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?
  Permalink  

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



Advertise | Privacy | Mobile
Web01 | 2.8.150428.2 | Last Updated 9 Jan 2013
Copyright © CodeProject, 1999-2015
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