views:

40

answers:

2

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 need to set this row as the first row in the combobox.

+2  A: 

If the (SelectedValue of the) ComboBox is not databound, all you need is to set `cbBrandForModel.SelectedIndex = 0;'

Henk Holterman
It worked! I set cbBrandForModel.SelectedValue = 0; :)
Ivan Stoyanov
That works too because you inserted the Id as "0". Whatever is most convenient.
Henk Holterman
+1  A: 

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.

msergeant