Adding a new item to a windows form application combo box without touching the datasource
I need to add a new item to a windows form application combo box without touching the datasource, like we add it in a web application, using开发者_开发技巧 Items.Insert(index, newItem).
Thanks
I would think this would do the trick - after you bind the data source to the ComboBox, add the additional items:
CombBox1.Items.Insert(0, "New Item");
Are you having problems getting that to work?
EDIT Did some research via google - turns out you can't add items after the ComboBox is databound. You have to add the items to the datasource itself. For example, say you're binding a DataTable to the ComboBox, with the DisplayMember set to column 1 and the ValueMember set to column 0:
DataRow newRow = myDataTable.NewRow();
newRow[0] = "0";
newRow[1] = "aa";
myDataTable.Rows.InsertAt(newRow, 0);
cb1.DataSource = myDataTable;
There is no drop down list
in windows forms however you maybe referring to ComboBox
, so use myComboBox.Items.Add
or myComboBox.Items.AddRange
or myComboBox.Items.Insert
精彩评论