user3191666
user3191666

Reputation: 159

Add default Item to data bound ComboBox

I have a function that brings back a datatable [GetDrinks()]

I use the function to populate a data source.

I want add a default value 'Select Drink' but it doesn't seem to work as it only shows the values form the datagrid.

Any ideas to get around this?

cboDrinks.DataSource = GetDrinks()
cboDrinks.DisplayMember = "Drink_DESCN"
cboDrinks.ValueMember = "Drink_ID"

cboDrinks.Items.Insert(0, " Select Drink ")
cboDrinks.SelectedIndex = 0

Upvotes: 1

Views: 1481

Answers (1)

LarsTech
LarsTech

Reputation: 81610

Per my comment, you need to insert the data into the DataTable that you are getting from GetDrinks:

Dim dt As DataTable = GetDrinks()
Dim row as DataRow = dt.NewRow
row("Drink_ID") = 0
row("Drink_DESCN") = " Select Drink "
dt.Rows.InsertAt(row, 0)

cboDrinks.DisplayMember = "Drink_DESCN"
cboDrinks.ValueMember = "Drink_ID"
cboDrinks.DataSource = dt
cboDrinks.SelectedIndex = 0

Note: Set the DisplayMember and ValueMember before the DataSource to avoid multiple refresh calls to the control.

Upvotes: 2

Related Questions