Click here to Skip to main content
15,884,064 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.
combobo1.items.add(0,"select")

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
 ds.AcceptChanges();
 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
Posted
Updated 17-Apr-12 19:37pm
v3

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
cb1.DataSource=dt;
cb1.DisplayMember="District"

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
 
v2
SQL
You should use !!

combobo1.Items.Insert(0,new ComboItem("--select--","-1"));
 
Share this answer
 
Comments
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    
Try-----
combobo1.Items.Insert(0,new ListItem("--select--","-1"));
Arav Pradeep Gupta 18-Apr-12 2:49am    
Hey, did you get working ................!!!

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