• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 614
  • Last Modified:

databound combobox.text being overwritten

Hi experts,
In summary my problems is,
Updating/populating a comboboxes' data bound items results in the first item of the resulting databound list being displayed in the combobox, overwriting the existing displayed value.

I have a simple data form which has several textboxes that are all bound to an sql table (DATATABLE). On this form I also have a combobox, it to has its text control bound to DATATABLE. The combobox uses data bound items in its drop down list. These items are from a seperate sql table (LOOKUPTABLE).

The records that are loaded into LOOKUPTABLE are dynamic and are the result of a query which uses other values currently being displayed in the textboxes on the dataform.

My problem is that when the LOOKUPTABLE has its items updated/populated, the value currently displayed in the combobox is overwritten with the first value in the data bound list.

Hope this makes sense.
Thanks in advance for any help.
1 Solution
This is desired behaviour. Only one of the items in the list could be selected. What is the DropDownStyle set to? Is it List?
Yes, this is an annoying feature of ComboBox control when it's bound to two different data sources (one for the SelectValue/Text, and one for the DataSource).   The only real work around is to capture the existing value of the Text property before rebinding the DataSource, and then reapply it after the binding has taken place.
Below is a trivial example:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DataSet1.Employee' table. You can move, or remove it, as needed.
        Dim ta As New DataSet1TableAdapters.OrganizationTableAdapter
        ' load the combobox with the starting values
        cbOrg.DataSource = Me.DataSet1.Organization
        cbOrg.DisplayMember = "OrgName"
        cbOrg.ValueMember = "OrgID"
    End Sub
    ' simulate a change to the Org list by rebinding the control
    Private Sub Rebind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim temp As String
        ' stash the existing text (before rebinding!)
        temp = cbOrg.Text
        ' provide a new binding source
        Dim ta As New DataSet1TableAdapters.OrganizationTableAdapter
        cbOrg.DataSource = Me.DataSet1.Organization
        ' re-apply the text
        cbOrg.Text = temp
    End Sub

Open in new window

MickDoevAuthor Commented:
Thanks graye,
What a frustrating few hours I had trying to "fix" a problem that apparantly can't be fixed !
I have implemented the capture / recall method to work around.
Many thanks MickD.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now