databound combobox.text being overwritten

Posted on 2009-02-10
Last Modified: 2012-08-13
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.
Question by:MickDoev
    LVL 83

    Expert Comment

    This is desired behaviour. Only one of the items in the list could be selected. What is the DropDownStyle set to? Is it List?
    LVL 41

    Accepted Solution

    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


    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    This video discusses moving either the default database or any database to a new volume.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now