none
Conecting two ComboBoxs RRS feed

  • Question

  • I making some project using Windows Forms Application in Visual Studio.

    I have conected Access date base to ComboBox1 and ComboBox2 (First column to ComboBox1; 2nd column to ComboBox2)  but if I expand the list I see every record.

    What should I write/do  when I want to have on ComboBox2 only record from Access conected with first column:

    Paper - Blue

    Metal - Green

    Wood - Orange

    Paper - Green


    So if I chose in ComboBox1 Paper I will be have in ComboBox2:

    - Blue

    - Green

    Thursday, December 6, 2018 9:28 AM

All replies

  • Hello,

    Since reading data from a ms-access table results in a DataTable (of course you can read into a DataSet but each table results in a DataTable so we are back to a DataTable) I mocked up to tables below.

    ''' <summary>
    ''' Mocked data that would come from ms-access
    ''' </summary>
    Public Class DataOperations
        Public Function ReadTable1() As DataTable
            Dim dt As New DataTable
            dt.Columns.Add(New DataColumn() With {.ColumnName = "Id", .DataType = GetType(Integer)})
            dt.Columns.Add(New DataColumn() With {.ColumnName = "Value", .DataType = GetType(String)})
    
            dt.Rows.Add(1, "Paper")
            dt.Rows.Add(2, "Metal")
            dt.Rows.Add(3, "Wood")
            dt.Rows.Add(4, "Green")
    
            Return dt
    
        End Function
        Public Function ReadTable2() As DataTable
            Dim dt As New DataTable
            dt.Columns.Add(New DataColumn() With {.ColumnName = "Id", .DataType = GetType(Integer)})
            dt.Columns.Add(New DataColumn() With {.ColumnName = "Table1Id", .DataType = GetType(Integer)})
            dt.Columns.Add(New DataColumn() With {.ColumnName = "Value", .DataType = GetType(String)})
    
            dt.Rows.Add(1, 1, "Blue")
            dt.Rows.Add(2, 2, "Orange")
            dt.Rows.Add(3, 1, "Green")
            dt.Rows.Add(4, 3, "Blue")
            dt.Rows.Add(5, 3, "Chocolate")
            dt.Rows.Add(6, 4, "Blue")
            dt.Rows.Add(7, 4, "Purple")
    
            Return dt
    
        End Function
    End Class
    

    Form code

    Public Class Form1
        Private Table2 As New DataTable
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim ops = New DataOperations
            Table2 = ops.ReadTable2
    
            ComboBox1.DataSource = ops.ReadTable1
            ComboBox1.DisplayMember = "Value"
            ComboBox1.ValueMember = "Id"
    
            AddHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
            ComboBox2.DisplayMember = "Value"
            ComboBox2.ValueMember = "Table1Id"
            HandleChanges()
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
            HandleChanges()
        End Sub
        Private Sub HandleChanges()
            Table2.DefaultView.RowFilter = $"Table1Id = {CInt(ComboBox1.SelectedValue)}"
            ComboBox2.DataSource = Table2
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, December 6, 2018 10:47 AM
    Moderator
  • Here is another twist on what you asked for with the same results with an additional field. There is even another twist on these two but will leave it to the two.

    Public Class DataOperations
        Public Function ReadTable1() As DataTable
            Dim dt As New DataTable
            dt.Columns.Add(New DataColumn() With {.ColumnName = "Id", .DataType = GetType(Integer)})
            dt.Columns.Add(New DataColumn() With {.ColumnName = "ColorId", .DataType = GetType(Integer)})
            dt.Columns.Add(New DataColumn() With {.ColumnName = "Value", .DataType = GetType(String)})
    
            dt.Rows.Add(1, 1, "Paper")
            dt.Rows.Add(2, 2, "Metal")
            dt.Rows.Add(3, 3, "Wood")
            dt.Rows.Add(4, 3, "Green")
            dt.Rows.Add(5, 2, "Wood")
            dt.Rows.Add(6, 1, "Paper")
    
            Return dt
    
        End Function
        Public Function ReadTable2() As DataTable
            Dim dt As New DataTable
            dt.Columns.Add(New DataColumn() With {.ColumnName = "Id", .DataType = GetType(Integer)})
            dt.Columns.Add(New DataColumn() With {.ColumnName = "ColorId", .DataType = GetType(Integer)})
            dt.Columns.Add(New DataColumn() With {.ColumnName = "Value", .DataType = GetType(String)})
    
            dt.Rows.Add(1, 1, "Blue")
            dt.Rows.Add(2, 2, "Orange")
            dt.Rows.Add(3, 1, "Green")
            dt.Rows.Add(4, 3, "Blue")
            dt.Rows.Add(5, 3, "Chocolate")
            dt.Rows.Add(7, 4, "Purple")
    
            Return dt
    
        End Function
    End Class

    Form code

    Public Class Form1
        Private Table2 As New DataTable
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim ops = New DataOperations
            Table2 = ops.ReadTable2
    
            ComboBox1.DataSource = ops.ReadTable1
            ComboBox1.DisplayMember = "Value"
            ComboBox1.ValueMember = "ColorId"
    
            AddHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
            ComboBox2.DisplayMember = "Value"
    
            HandleChanges()
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
            HandleChanges()
        End Sub
        Private Sub HandleChanges()
            Table2.DefaultView.RowFilter = $"ColorId = {CInt(ComboBox1.SelectedValue)}"
            ComboBox2.DataSource = Table2
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by Alex Li-MSFT Tuesday, January 1, 2019 7:27 AM
    Thursday, December 6, 2018 11:02 AM
    Moderator
  • Hello,

    Just checking in to see how this was going and if the issue has been resolved or not. If resolved please mark a reply as answered so to help others with a similar issue and it not resolved let us know.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, December 13, 2018 12:14 PM
    Moderator