Click here to Skip to main content
15,663,341 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
hi i want to insert an item called select to combobox at runtime.the combobox
is already bound to the database.if try this code.

it will display an error message item collection cannot be modified .

and i tried an second option
ds.Tables[0].Rows.Add(label4.Text);//ds is dataset
 objadapter.Update(ds.Tables[0]);objadapter is sqldataadapter
dd1.Fill(ds, "teacherdetailloading");
               combobox1.DataSource = ds.Tables[teacherdetailloading"];
               combobox1.DisplayMember = sub1;
               combobox1.ValueMember = sub1;

i am using this combobox inside datagridview which has slno column int type.
because it will display error message input string was not in correct format.
i tried casting but it will not work

if any one have any idea about it help me with some ex coding or link
tahnks in advance
Updated 17-Apr-12 19:37pm

You should use !!

combobo1.Items.Insert(0,new ComboItem("--select--","-1"));
Share this answer
praveenkumar78 18-Apr-12 1:57am    
hi pradeep
iam using visual studio 2005 so i don't find an option
comboitem.if there is any other option help me
Member 9377809 12-Sep-13 3:49am    
hi pradeep
iam using visual studio 2010 so i don't find an option
comboitem or ListItem if there is any other option help me
"cmbFiscalYear.Items.Insert(0, new ListItem("--select--", "-1"))"

I want to add a new Item into the cmbfiscalyear

Mahesh T J 12-09-2013
Arav Pradeep Gupta 18-Apr-12 2:10am    
combobo1.Items.Insert(0,new ListItem("--select--","-1"));
Arav Pradeep Gupta 18-Apr-12 2:49am    
Hey, did you get working ................!!!
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

cb1.DataSource=ds.Tables[0]  or   cb1.DataSource=dt;    // where dt=ds.Tables[0];

Go with the code as below:

DataTable Dt1=new DataTable();

again set like

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

This will solve your Problems

[edit] Added code tags [/edit]
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