Click here to Skip to main content
16,017,755 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to add a --Select-- option in combobox in c#.net windows application. i am using visual studio 2008. in this combobox have already datasouce how can i add the select option in this combobox.
i tried
combobox1.Items.Insert(0,"select") and combobox1.Items.add(0 new ListItem("0","select"));
but it's not working. please help.




thanks in advance
Posted
Comments
DamithSL 6-May-14 4:40am    
how you data bind? please update the question with your code

1 solution

This will insert new item in combobox
CombBox1.Items.Insert(0, "New Item");
Remember one thing you can not add item once datasource is set. So, you add item in datasource itself. like:
DataTable dtCombo = new DataTable();
DataRow drow;
dtCombo.Columns.Add("Id",typeof(int));
dtCombo.Columns.Add("Name",typeof(string));
drow = dtCombo.NewRow();
drow[0]=1;
drow[1]="One";
dtCombo.Rows.Add(drow);
DataRow drow2 = dtCombo.NewRow();
drow2[0] = 2;
drow2[1] = "Select";

dtCombo.Rows.InsertAt(drow2, 0);
dtCombo.AcceptChanges();

cmbTest.DisplayMember = "Name";
cmbTest.ValueMember = "Id";
cmbTest.DataSource = dtCombo.DefaultView;
 
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