Ivan Stoyanov
Ivan Stoyanov

Reputation: 5482

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

Upvotes: 0

Views: 1357

Answers (2)

msergeant
msergeant

Reputation: 4801

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.

Upvotes: 1

Henk Holterman
Henk Holterman

Reputation: 273209

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

Upvotes: 2

Related Questions