Click here to Skip to main content
13,150,178 members (60,602 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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
Posted 17-Apr-12 19:33pm
Updated 17-Apr-12 19:37pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You should use !!
combobo1.Items.Insert(0,new ComboItem("--select--","-1"));
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
Gupta Pradeep MCA 18-Apr-12 2:10am
combobo1.Items.Insert(0,new ListItem("--select--","-1"));
Gupta Pradeep MCA 18-Apr-12 2:49am
Hey, did you get working ................!!!
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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]

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170924.2 | Last Updated 20 Apr 2012
Copyright © CodeProject, 1999-2017
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