Link to home
Start Free TrialLog in
Avatar of TSFLLC
TSFLLC

asked on

Why do my comboboxes display a value instead of being blank until a value is chosen from dropdown?

I'm rewriting a MSAccess application to VB.NET for more functionality.  I have come a long way with my understanding
but I'm puzzled with what is probably a simple fix for VB.NET novices.

I have a "Clear / New" button on a form for initializing all fields on the form in preparation for data entry of new record.
I have several combo boxes that are required fields and several that are not.  I do not want any of the combo boxes to
display a value until the user click's the dropdown and choses value.  Why do they all contain the first value from the
datatable instead of being blank...AND...more importantly, how do I clear these values until the control is chosen?

Below is my code in various parts:

Public daContacts As New System.Data.OleDb.OleDbDataAdapter
Public dsContacts As New DataSet
Public dtContacts1 As New DataTable
Public dtContacts2 As New DataTable
Public arrMrMrs() As String = {"Mr.", "Mrs.", "Ms.", "Dr."}

daContacts.SelectCommand = New System.Data.OleDb.OleDbCommand("SELECT * FROM Contacts ORDER BY ContactName", TSFConnection)
daContacts.Fill(dsContacts)
dtContacts1 = dsContacts.Tables(0)
dtContacts2 = dsContacts.Tables(0)

cmboBuyer.DataSource = dtContacts1
cmboBuyer.ValueMember = "RecID"
cmboBuyer.DisplayMember = "ContactName"

cmboSeller.DataSource = dtContacts2
cmboSeller.ValueMember = "RecID"
cmboSeller.DisplayMember = "ContactName"

cmboMrMrs.DataSource = arrMrMrs
Avatar of Vulten
Vulten
Flag of Sweden image

Have you tried Combobox1.Text=""

That ought to clear it for you
oh... and one more thing... Use

        ComboBox1.Text = ""
        ComboBox1.SelectedIndex = -1 '(Meaning NO item should be selected)

Just to make sure that nothing is selected in the Combobox1... Since It will maintain any selection even if Combobox1.Text =""
Avatar of TSFLLC
TSFLLC

ASKER

I found both of these answers previously and neither one of them worked because they are bound to a datatable.

I did find this piece of code and it does work.
However, I don't like it for (2) reasons:

1) An inordinate amount of code to clear a field.
2) An empty first record when you click on the dropdown.

But it does work....maybe someone else has a better way.

        Dim dr As DataRow
        dr = dsContacts1.Tables(0).NewRow
        dr.Item("ContactIndex") = ""
        dsContacts1.Tables(0).Rows.InsertAt(dr, 0)

        dr = dsContacts2.Tables(0).NewRow
        dr.Item("ContactIndex") = ""
        dsContacts2.Tables(0).Rows.InsertAt(dr, 0)

        cmboSeller.SelectedIndex = 0
        cmboBuyer.SelectedIndex = 0

In MS Access, even though you have a datasource of a SQL statement......
It is simply a matter of cmboseller.text = "" to resolve the problem.

Thanks!
Avatar of TSFLLC

ASKER

OOPS!   Where I wrote 'ContactIndex'....I meant 'ContactName'
ASKER CERTIFIED SOLUTION
Avatar of jake072
jake072
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial