Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 334
  • Last Modified:

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,
0
Rog D
Asked:
Rog D
  • 7
  • 4
  • 4
  • +2
1 Solution
 
zofcentrCommented:
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

0
 
BuggyCoderCommented:
combo1.selecteditem=combo2.Text;
0
 
Rog DManager Inforamtion SystemsAuthor Commented:
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.
0
Technology Partners: 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!

 
Rog DManager Inforamtion SystemsAuthor Commented:
BuggyCoder,

combo1.selecteditem=combo2.Text;

When I do this in the combo2 selectedIndexChanged event, the text is ""
0
 
BuggyCoderCommented:
I am sorry, do this:-
comboBox1.SelectedValue = comboBox2.Text;

Open in new window

0
 
zofcentrCommented:
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.
0
 
Rog DManager Inforamtion SystemsAuthor Commented:
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.
0
 
Rog DManager Inforamtion SystemsAuthor Commented:
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.
0
 
BuggyCoderCommented:
the code below has been tested by me on a button_click event:-
comboBox1.SelectedValue = comboBox2.Text;

Open in new window

0
 
CodeCruiserCommented:
Show us the code you use to populate the comboboxes.
0
 
zofcentrCommented:
Rog, please supply your code.

In my example Property Sorted for combobxes is disabled.
Did you try to sort data when loading into datatable?
0
 
Éric MoreauSenior .Net ConsultantCommented:
have you taken a look at my link? This question should have been answered by now!
0
 
Rog DManager Inforamtion SystemsAuthor Commented:
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
0
 
BuggyCoderCommented:
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

0
 
zofcentrCommented:
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.
0
 
Rog DManager Inforamtion SystemsAuthor Commented:
I need to have the combo boxes sorted.  One on PartNO and the other on PartNoDesc.

So when you change the comboboxes your solution will NOT work, as it tries to match the
index NOT the picked item.

I think the issue is more in the SelectedIndexChanged event and not causing one change to call the other combo's SelectedIndexChanged event to fire and create a circle.

I have something like this for that scenario, but it isn't working yet...

 Private Sub cboPartNumber_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboPartNumber.SelectedIndexChanged
        'Dim rptProduction As Integer, myIndex As Integer

        If Not bpartpicked Then
            'cboDes.SelectedIndex = cboPartNumber.SelectedIndex
            If Not IsNothing(cboPartNumber.SelectedValue) Then
                bpartpicked = True
                bdeskpicked = True
                cboDes.SelectedValue = DirectCast(cboPartNumber.SelectedItem, DataRowView).Item("PartNo")
            End If
            bpartpicked = False
        End If
    End Sub


    Private Sub cboDes_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboDes.SelectedIndexChanged

        If Not bdeskpicked Then
            'cboPartNumber.SelectedIndex = cboDes.SelectedIndex
            If Not IsNothing(cboDes.SelectedValue) Then
                bpartpicked = True
                bdeskpicked = True
                cboPartNumber.SelectedValue = DirectCast(cboDes.SelectedItem, DataRowView).Item("PartNoDesc")

            End If
            'bdeskpicked = False
        End If
    End Sub
0
 
Rog DManager Inforamtion SystemsAuthor Commented:
No real answer was given.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 7
  • 4
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now