Вопрос

I have a combobox that is bound to a DB and works great.

However, when the user adds a value to the table to which the combobox is bound, the combobox keeps the old value and adds the current items in the table.

In this case the table originally held just one record ("2012"). The user added 2013 so the table now shows two records ("2012" and "2013"), but the combobox shows three records ("2012", "2012", and "2013"). UNTIL I exit the app and restart, in which case it correctly reflects the only two records in the table ("2012" and "2013").

I have tried cboYear.Datasource = Nothing, cboYear.items.clear, cboYear.DataBindings.clear and nothing works.

Here is the code:

Try
    Dim asql As String = ("SELECT * FROM YearsAvailable ORDER BY CurrentYear")
    Dim da As New OleDbDataAdapter(asql, con)
    da.Fill(ds)

    cboYear.ValueMember = "CurrentYear"
    cboYear.DataSource = ds.Tables(0)
    cboYear.SelectedIndex = 0
    CurrentYear = cboYear.Text
    Me.Text = "MSD of Perry Township Compensation Model: " & CurrentYear
Catch ex As Exception
    MsgBox("ERROR filling the YEAR control: " & ex.Message.ToString)
End Try
Это было полезно?

Решение

You need to clear ds before Fill it again:

ds.Clear();
da.Fill(ds);

Check this out: DataAdapter.Fill

You can use the Fill method multiple times on the same DataTable. If a primary key exists, incoming rows are merged with matching rows that already exist. If no primary key exists, incoming rows are appended to the DataTable.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top