Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi in my application combobox is bound to the database.i want to an item
at runtime to that combobox if i try to add it will display an error message
item collection cannot be modified.if anyone have any idea how to do it.
help me with some code examples

thanks in advance.
Posted

Hi,

This error occurs when you have set data source to the ComboBox and try to change ComboBox items.

You should change the data source instead of changing ComboBox items.

Here is a solution that might help you:
http://shawpnendu.blogspot.in/2010/06/error-items-collection-cannot-be.html[^]
 
Share this answer
 
Its Pretty Simple

You have to do nothing

when you bound your combobox from the database then for adding a new item into that combobox you must change the datasource.

Let's see an example to make it better.


Suppose cb1 is your combobox and the field name is District in Database.

when you bound it from databse it shows name of district, Now you want to add some run time values.

from Database you set the datasource like

C#
cb1.DataSource=ds.Tables[0]  or   cb1.DataSource=dt;    // where dt=ds.Tables[0];
cb1.DisplayMember="District";


Go with the code as below:

C#
DataTable Dt1=new DataTable();
Dt1.Columns.Add("District");
Dt1.Rows.Add("Delhi");
Dt1.Rows.Add("Haryana");


Now
C#
dt.Merge(Dt1);


again set like
C#
cb1.DataSource=dt;    
cb1.DisplayMember="District";


This will add two new Districts named Delhi and Haryana in ComBoBox

This will solve your Problems
 
Share this answer
 
v3
Clear the combox first add new
 
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