pankaj babbar
pankaj babbar

Reputation: 69

Can we bind one combobox to multiple value members?

I am trying to display column 1 and column2 of two different SQL tables In single combobox vertically. In my case it is showing y table col3 but not showing x table col 1 in combobox. Is it possible to assisgn multiple value members to single combobox or not ?

this code is just to explain you how I am trying.

                    ComboBox1.ValueMember = "x table col1"
                    ComboBox1.ValueMember = "y table col3"
                  
                   
               

Upvotes: 0

Views: 371

Answers (1)

Mary
Mary

Reputation: 15091

I used 2 select strings with 2 commands (a single connection assuming they are both in the same database). Both readers add their data to the same list which is the data source for the combo box.

Private Sub OPCode()
    Dim s = "Select col1  From TableX;"
    Dim lst As New List(Of String)
    Using cn As New SqlConnection(My.Settings.CoffeeConnection)
        Using cmd As New SqlCommand(s, cn)
            cn.Open()
            Using reader = cmd.ExecuteReader
                While reader.Read
                    lst.Add(reader.GetString(0))
                End While
            End Using
        End Using
        s = "Select col3 From TableY"
        Using cmd2 As New SqlCommand(s, cn)
            Using reader = cmd2.ExecuteReader
                While reader.Read
                    lst.Add(reader.GetString(0))
                End While
            End Using
        End Using
    End Using
    ComboBox1.DataSource = lst
End Sub

EDIT
Do you mean something like this?

Dim s = "Select col1, col2 From TableX"

Then the addition to the list would look like this.

lst.Add($"{reader.GetString(0)}, {reader.GetString(1)}")

Upvotes: 1

Related Questions