Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How To: Add a blank value to ComboBox item collection, when ComboBox is bound to a data source.

How-to

How To: Add a blank value to ComboBox item collection, when ComboBox is bound to a data source.

by  PankajBanga  Posted    (Edited  )
In VB6 you could set the initial value held in ComboBox control to -1 so that it is blank to begin with. But in .NET if when the DataSource property is set for a ComboBox, you cannot modify the item collection. You cannot set ComboBox1.Text = "" or use Add method to add a new item to the list.

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("")


Happy Programming!!!
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top