Results 1 to 4 of 4
  1. #1
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    RowSource in VB.NET Comboboxes?

    I have been setting DataSource for a series of combo boxes to the same table as they all display the same options. However, when I ran the project, any change in any one combo box makes all the others 'flip' to the same selected value. This is obviously wrong.

    In Access, you have 'Control Source' which is the db field that is bound to the combo box and 'Row Source' can be a query or whatnot that several combo boxes can run 'independently'.

    Is there an equivalent option in VB.NET? I DO NOT want to manually enter the exact same options many times as that is hard to maintain (and stupid, IMHO).

    Just to repeat: it is not going to work to say DataSource = Row Source. if two combo boxes are set to the same data source, a selection in one automatically makes the other one select the same value.

    Thanks in Advance!

  2. #2
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts
    this issue has been resolved.

  3. #3
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Steve,

    Would you please post your solution for the benefit of the other loungers. Thanks.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  4. #4
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Pleased to be of service!

    here's the situations: on a windows form, you have several combo boxes with standard items (such as Yes, No, N/A). In Access, you can create a lookup table and bind the control itself to some other table. In .NET there is no corresponding RowSource setting. If you use the same data source for a series of combo boxes to the same lookup table, the application will also 'bind' every single comb box, meaning selecting Yes in one selects Yes in all the others.

    I believe this happens because .NET has a setting called 'CurrencyManager' that handles data binding for any instance that where it is declared, even if the data source is on multiple controls.

    So, instead of just setting the controls to the same data source, you need to do something like this in the code-behind:

    Use the same datasource but prevent the controls from interacting:

    Me.ComboBox1.DataSource = New DataView(_MyDatabase_1DataSet.MyDataTable) <-- note the keyword 'New'
    Me.ComboBox2.DataSource = New DataView(_MyDatabase_1DataSet.MyDataTable) <-- note the keyword 'New'

    Automatic assignation:
    In my case, I have many combo boxes that need to be bound in this fashion. rather than copy/paste a bunch of lines like above, I created the following loop:

    Dim obj As Object
    Dim cmb As ComboBox

    For Each obj In MainTab.TabPages(1).Controls

    With obj
    If LCase(TypeName(obj)) = "combobox" Then <-- you can change this to textbox or listbox, etc.

    cmb = CType(obj, ComboBox) <-- must do a cast to set properties for the control you want to manipulate

    Dim ds As New DataView(_MyDatabase_1DataSet.MyDataTable) <-- note keyword New

    cmb.DataSource = ds
    cmb.DisplayMember = "RangeValue" <-- for combo box, you have to set the display and
    cmb.ValueMember = "RangeValue" <-- the stored value

    End If

    End With

    Next

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •