Bound ComboBox Data Error

I have written a program that binds controls on a form to fields of a table in a Microsoft Access database. One of the controls on the form is a combo box that contains three names in its Items property. I use BindingSource filtering to change from one record to another. When I change from one record to another, if the new record has nothing in the combo box control then the data that had been in the combo box for the previous record is displayed for the new record and appears as though it was either selected or had been stored in the database. If a record already has legitimate data for the combo box it displays properly and there is no problem with data from the previous record being copied, it only happens when a record without any combo box data is displayed.

I have noticed that the problem can be prevented by adding an empty line to the beginning of the Items property, but I do not like how this looks. I am hoping that someone knows how to prevent the combo box from retaining data when it should be blank.

Below is the data setup the binding source and the combo box binding:

Code Snippet

.BindingSource1 = New BindingSource

With .BindingSource1

.DataSource = Me.aDataSet

.DataMember = "Data"

.Sort = "ID"

.MoveLast()

.MoveFirst()

End With

Me.ComboBox1.DataBindings.Add("text", BindingSource1, "LName", False, DataSourceUpdateMode.OnValidation)

[2544 byte] By [Mario1776] at [2008-1-5]
# 1
I had something kinda sorta similar. When I put in Error trapping, I was able to control the output.
cwillsh at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

Hi,

Based on your post, I give you the simple code. It binds ComboBox control to field of datatable. It uses Binding class to bind ComboBox.Text property to field of datatable.

After I change ComboBox.Text property to bind other field of datatable. If new field is null, ComboBox will display blank.

If you are convenient, you can post some related code. We will know the more about your program. Then we can give you better advice.

Code Snippet

Dim bd As Binding

Dim ds As DataSet = New DataSet

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles

Button1.Click

Dim connectionString As String = "Provider=Microsoft.Jet.OleDb.4.0;Data Source="

Dim con As OleDbConnection = New OleDbConnection(connectionString + "c:\northwind.mdb")

con.Open()

Dim da As OleDbDataAdapter = New OleDbDataAdapter(customers, con)

da.Fill(ds, "Customers")

DataGridView1.DataSource = ds

DataGridView1.DataMember = "Customers"

bd = New Binding("Text", ds, "Customers.CustomerID")

Me.ComboBox1.DataBindings.Add(bd)

con.Close()

End Sub

Me.ComboBox1.DataBindings.Remove(bd)

bd = New Binding("Text", ds, "Customers.Region")

Me.ComboBox1.DataBindings.Add(bd)

Hope this can help you.

Riquel

RiquelDong–MSFT at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic Language...