Friday, 26 November 2010

VB.net - Defaulting a databound ComboBox to show a blank value

 

Once a Combobox is bound to a datasource, we no longer have control over it’s List Collection, so we can’t add a blank item directly to the Combobox’s Collection using the Add Method.

However, we can temporariliy add a row having a blank value in the field bound to the Combobox’s DisplayMemebr property. We can then set the Selected index of the Combobox to that of the new “Blank” row…

dim drwBlankDataRow as DataRow


’ Databind Combobox

cmbComboBox.DataSource = dtsDataSet.tblTable
cmbComboBox.ValueMember = “ID_Column”
cmbComboBox.DisplayMember = “Display_Column”


’ Add a blank row to the DataSet

drwBlankDataRow = dtsDataSet.tblTable.NewRow
drwBlankDataRow(“Display_Column”) = “”
dtsDataSet.tblTable.AddtblTableRow(drwBlankDataRow)


’ Default the ComboBox to the Blank Row

cmbComboBox.SelectedIndex = cmbComboBox.FindStringExact(“”)

The Combobox will now show the new “Blank” row value.

No comments:

Post a Comment