×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Visual Basic (Classic) FAQ

How-to

How To: Add a blank value to ComboBox item collection, when ComboBox is bound to a data source. by PankajBanga
Posted: 6 Jul 04

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!!!

Back to Visual Basic (Classic) FAQ Index
Back to Visual Basic (Classic) Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close