Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WinForm
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&quot];
               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
Edited 17-Apr-12 19:37pm
sravani.v15.7K
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You should use !!
 
combobo1.Items.Insert(0,new ComboItem("--select--","-1"));
  Permalink  
Comments
praveenkumar78 at 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 at 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 at 18-Apr-12 2:10am
   
Try-----
combobo1.Items.Insert(0,new ListItem("--select--","-1"));
Gupta Pradeep MCA at 18-Apr-12 2:49am
   
Hey, did you get working ................!!!
Rate this: bad
good
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];
cb1.DisplayMember="District"'
 
Go with the code as below:
 
DataTable Dt1=new DataTable();
Dt1.Columns.Add("District");
Dt1.Rows.Add("Delhi");
Dt1.Rows.Add("Haryana")
Now
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]
  Permalink  
v2

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.140709.1 | Last Updated 20 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid