Setting first field in a combo box to null

18,800

Solution 1

This is the code I used to overcome the problem...

ComboBox1.SelectedValue = -1

Solution 2

ComboBox1.SelectedValue = -1 

didn't work for me but this did:

ComboBox1.SelectedIndex = -1 

I would have though -1 would have been an invalid index value, but obviously not.

Kristian

Solution 3

You can insert a blank entry into data source.

Public Class Data
 Public Property No() As Integer
 Public Property Name() As String
End Class

and List(of Data),

Dim mlist As New List(Of Data)() From
    {
        New Data() With {.Name = "", .No = 0},
        New Data() With {.Name = "One", .No = 1},
        New Data() With {.Name = "Two", .No = 2}
    }

ComboBox1.DataSource = mlist
ComboBox1.DisplayMember = "Name"
ComboBox1.ValueMember = "No"
Share:
18,800
user765942
Author by

user765942

Updated on June 20, 2022

Comments

  • user765942
    user765942 almost 2 years

    I have a databound ComboBox on my form. Is there any way that I can make the first field blank.

    I can do this with a DropDownList in the HTML part of .Net but is there a way to do it for a ComboBox?

    Thanks

  • user765942
    user765942 over 12 years
    what I did was ComboBox1.SelectedValue = -1
  • Deeptechtons
    Deeptechtons over 12 years
    @AVD actually your answer is what answers his question, thing is he had framed his question wrong