Solved

VB.Net linking two comboboxes to eachother...

Posted on 2012-03-19
18
278 Views
Last Modified: 2012-04-30
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
Comment
Question by:Rog
  • 7
  • 4
  • 4
  • +2
18 Comments
 
LVL 3

Expert Comment

by:zofcentr
ID: 37737856
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
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37737869
combo1.selecteditem=combo2.Text;
0
 
LVL 8

Author Comment

by:Rog
ID: 37737870
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
 
LVL 8

Author Comment

by:Rog
ID: 37737878
BuggyCoder,

combo1.selecteditem=combo2.Text;

When I do this in the combo2 selectedIndexChanged event, the text is ""
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 37737937
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37737949
I am sorry, do this:-
comboBox1.SelectedValue = comboBox2.Text;

Open in new window

0
 
LVL 3

Expert Comment

by:zofcentr
ID: 37738108
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
 
LVL 8

Author Comment

by:Rog
ID: 37738125
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
 
LVL 8

Author Comment

by:Rog
ID: 37738172
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 20

Expert Comment

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

Open in new window

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37740107
Show us the code you use to populate the comboboxes.
0
 
LVL 3

Expert Comment

by:zofcentr
ID: 37741024
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
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 37741379
have you taken a look at my link? This question should have been answered by now!
0
 
LVL 8

Author Comment

by:Rog
ID: 37742221
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
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37742252
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
 
LVL 3

Expert Comment

by:zofcentr
ID: 37742461
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
 
LVL 8

Accepted Solution

by:
Rog earned 0 total points
ID: 37743116
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
 
LVL 8

Author Closing Comment

by:Rog
ID: 37909660
No real answer was given.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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 …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) 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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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