How to add a row to databound combobox?
I have a combobox and I need the first row to be set as default.
This is my code
cbBrandForModel.DisplayMember = "BrandName";
cbBrandForModel.ValueMember = "BrandID";
cbBrandForModel.DataSource = dataTable;
I need to add this:
cbBrandForModel.DisplayMember = "Select Brand";
cbBrandForModel.ValueMember = "0";
Can anyone tell me how to do it?
EDIT: I managed to add a new row in my DataTable.
var dataRow = dataTable.NewRow();
dataRow["BrandID"] = "0";
dataRow["BrandName"] = "--Select Brand--";
dataTable.Rows.Add(dataRow);
Now I 开发者_如何学Pythonneed to set this row as the first row in the combobox.
If the (SelectedValue of the) ComboBox is not databound, all you need is to set `cbBrandForModel.SelectedIndex = 0;'
I agree with the answer regarding SelectedIndex.
In addition your second code snippet, where you set DisplayMember and ValueMember again, overwrites the first snippet. This is not going to have the effect you intend.
精彩评论