Avatar of Rog D
Rog DFlag for United States of America asked on

VB.Net linking two comboboxes to eachother...

I have an application that utilizes two comboboxes.  The comboboxes show data from the same table but different columns.

Combo  DataMember  DisplayMember

Combo1 PartNo             PartNoDesc
Combo2 PartNoDesc     PartNo

I tried the index route, but when I sort the data in the combo box this didn't fair so well.

When a user picks partno , I want to have the other combo select the same one's description.

I thought this was easy, but am fiding out it's a little tricky.

Thanks,
Visual Basic.NET

Avatar of undefined
Last Comment
Rog D

8/22/2022 - Mon
zofcentr

Try this:
   
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim dt As DataTable
        dt = New DataTable()
        dt.Columns.Add("category1")
        dt.Columns.Add("category2")

        Dim newCustomersRow As DataRow = dt.NewRow()
        newCustomersRow("category1") = "--Select Category1--"
        newCustomersRow("category2") = "--Select Category2--"
        dt.Rows.InsertAt(newCustomersRow, 0)
        newCustomersRow = dt.NewRow()
        newCustomersRow("category1") = "Item1"
        newCustomersRow("category2") = "Item1"
        dt.Rows.InsertAt(newCustomersRow, 1)
        newCustomersRow = dt.NewRow()
        newCustomersRow("category1") = "Item2"
        newCustomersRow("category2") = "Item2"
        dt.Rows.InsertAt(newCustomersRow, 2)
        With cmbcategory1
            .DisplayMember = "category1"
            .ValueMember = "category1"
            .DataSource = dt
        End With
        With cmbcategory2
            .DisplayMember = "category2"
            .ValueMember = "category2"
            .DataSource = dt
        End With

        cmbcategory1.Refresh()
        cmbcategory2.Refresh()
    End Sub

Open in new window

BuggyCoder

combo1.selecteditem=combo2.Text;
ASKER
Rog D

I do know how to fill the combo boxes, but wanted to know about the selection.

When I select one combo, I want the other combo to also select the same row.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER
Rog D

BuggyCoder,

combo1.selecteditem=combo2.Text;

When I do this in the combo2 selectedIndexChanged event, the text is ""
Éric Moreau

BuggyCoder

I am sorry, do this:-
comboBox1.SelectedValue = comboBox2.Text;

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
zofcentr

Hi Rog,
selection change is done automatically if you assign datetable as DataSource:

.DisplayMember = "category1"
.ValueMember = "category1"
[b].DataSource = dt[/b]

Open in new window


At least by defualt.
If you do not change this behaviour it works like that as this is the same datatable for both combobox-es and changing current row in one affects second combobox.
Just try in simple new created project and should work as in my.
ASKER
Rog D

Thanks ZofCentr....
I did try this out and am close,  Currently the combo boxes are now showing
System.Data.DataRowView in the dropdown.

I need to do a little investigation.

Thanks.
ASKER
Rog D

ZofCentr

Your solution does not work if the combo boxes are not sorted in the same order.

ie.  Combobox 1 sort by PartNo
      Combobox 2 sort by PartNoDesc

Then when I pick one, the other chagnes but not to the
correct item.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
BuggyCoder

the code below has been tested by me on a button_click event:-
comboBox1.SelectedValue = comboBox2.Text;

Open in new window

Nasir Razzaq

Show us the code you use to populate the comboboxes.
zofcentr

Rog, please supply your code.

In my example Property Sorted for combobxes is disabled.
Did you try to sort data when loading into datatable?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Éric Moreau

have you taken a look at my link? This question should have been answered by now!
ASKER
Rog D

Here is the code...

I have tried it with both combos binding to DT, but when you set teh sort property on both combo's the indexes then don't match.

Top of Form
   Dim dt as datatable
   Dim dt2 as datatable

Private Sub FillCombos()

                dt = dsRFOX.Tables("qryRF_MTL")
                dt2 = dsRFOX.Tables("qryRF_MTL")

                cboPartNumber.Items.Clear() 'CLEAR THE PARTNUMBERS
                cboDes.Items.Clear() 'CLEAR THE DESCRIPTIONS


                cboPartNumber.DataSource = dt
                cboPartNumber.DisplayMember = "PartNo"
                cboPartNumber.ValueMember = "PartNoDesc"
             
                cboDes.DataSource = dt2
                cboDes.DisplayMember = "PartNoDesc"
                cboDes.ValueMember = "PartNo"

end sub
BuggyCoder

don't search by index, search by value or Text
The Part No is display member, indexes do get modified when sorting is done....

Ideally, this should solve your problem:-
comboBox1.SelectedValue = comboBox2.Text;

Open in new window

Your help has saved me hundreds of hours of internet surfing.
fblack61
zofcentr

Rog,
I did change some code to be more like yours.

     
 Dim dt As DataTable
        Dim dt1, dt2 As DataTable
        Dim dsRFOX As DataSet
        dsRFOX = New DataSet
        dt = New DataTable("qryRF_MTL")
        dsRFOX.Tables.Add(dt)

        dt.Columns.Add("PartNo")
        dt.Columns.Add("PartNoDesc")

        Dim newCustomersRow As DataRow = dt.NewRow()
        newCustomersRow("PartNo") = "1"
        newCustomersRow("PartNoDesc") = "Descr 1"
        dt.Rows.InsertAt(newCustomersRow, 0)
        newCustomersRow = dt.NewRow()
        newCustomersRow("PartNo") = "2"
        newCustomersRow("PartNoDesc") = "Descr 2"
        dt.Rows.InsertAt(newCustomersRow, 1)
        newCustomersRow = dt.NewRow()
        newCustomersRow("PartNo") = "3"
        newCustomersRow("PartNoDesc") = "Descr 3"
        dt.Rows.InsertAt(newCustomersRow, 2)

        dt1 = dsRFOX.Tables("qryRF_MTL")
        dt2 = dsRFOX.Tables("qryRF_MTL")

        With cmbcategory1
            .DisplayMember = "PartNo"
            .ValueMember = "PartNoDesc"
            .DataSource = dt1
        End With
        With cmbcategory2
            .DisplayMember = "PartNoDesc"
            .ValueMember = "PartNo"
            .DataSource = dt2
        End With

Open in new window


And it still works in a way that when I change one combobox selection second one is updated with corresponding PartNo or PartNoDesc values.

And as I write earlier I have disabled sorting on both combo boxes (Property Sorted = False).

One suggestions to your code.

You do not need to define two variables dt and dt2 since all DateTable is reference type.
You can use use one eg. "dt" and assign to both combo boxes.
 cboPartNumber.DataSource = dt
  cboDes.DataSource = dt


If you code still does not work try create simple test project with simple form and just add two combo boxes.
Maybe there are other properties or events which change default behaviour described by me.
ASKER CERTIFIED SOLUTION
Rog D

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
Rog D

No real answer was given.