Senin, 20 Desember 2010

Add a blank value to ComboBox item collection, when ComboBox is bound to a data source

So how would you add a blank value to the ComboBox item collection, when it is bound to a data source?

The DataSource property of ComboBox accepts an object that implements the IList interface, such as a DataSet. Although you cannot modify the item collection, you can actually add a blank data row in DataSet which is bound to ComboBox. The new blank row will be shown in the ComboBox.

This is how you can do it:

'Populate ComboBox.
ComboBox1.DataSource = myDS.Tables(0)
ComboBox1.ValueMember = "ColumnName"
ComboBox1.DisplayMember = "ColumnName"

'Add a blank row.
DR = myDS.Tables(0).NewRow
DR(1) = ""
myDS.Tables(0).Rows.Add(DR)

'Initially show blank row in ComboBox.
ComboBox1.SelectedIndex = ComboBox1.FindStringExact("")

Tidak ada komentar:

Posting Komentar