Avatar of Jimbo99999
Jimbo99999
Flag for United States of America asked on

VB.net - 2 column "ComboBox" - what to use?

Good Morning Experts

Well, I thought I was home free with my little project.  However, I need to add a way to select an item from a list with the descritpion of it to the right. All I can think to call it is a multi-column ComboBox.  Since that is not possible, which control is best to mimic that behavior? ListBox, ListView, DataGridview? I need it to load it from SQL database with no column headings and first record showing with down arrow to right to expand to drop down rest of selections.

Can you please offer suggestion on how to achieve this?

Thanks,
jimbo99999
Visual Basic.NET

Avatar of undefined
Last Comment
Jimbo99999

8/22/2022 - Mon
Navneet Hegde

Hi!

Whu on't you try combo-box itself with text having Description as well seperated by '-'

Something Like

US - Country in America continent
China- Have highest population
.
.
etc


Thanks!
Jimbo99999

ASKER
Good Day

That would work. However, when loading it from SQL db I am unfamiliar with how to put the 2 columns of data together in the .net coding.  Basically I mean when I do my query I make the table and assign it to the datasource of the control.  I don't know how to tell it to put the two columns to gether before connecting to the datasource.

How do I achieve that?

Thanks,
jimbo99999
ASKER CERTIFIED SOLUTION
Navneet Hegde

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.
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
Jimbo99999

ASKER
Hello:

I really don't have any code yet as I am unfamiliar with combining the columns to make one for the combobox.  Then after user makes selection I would have to pull the first value off the front of the selection to then be part of a new record that I need to add to the database.

I am intrigued by your suggestions. I will try now to load the combobox.

jimbo99999
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Jimbo99999

ASKER
I cannot get it to work.  I am getting System.Data.DataRowView in the ComboBox.
Here is my code:

            '******************
            'Load SCAC ComboBox
            '******************
            Dim ConnectionString As New SqlConnectionStringBuilder()
            With ConnectionString
                .DataSource = "(local)"
                .InitialCatalog = "Test"
                .IntegratedSecurity = True
            End With
            SQLConnect = New System.Data.SqlClient.SqlConnection(ConnectionString.ConnectionString())
            cmdSelSCAC = New SqlCommand("Select SCAC " & _
                                                "from [CARRIER PROFILES] ", SQLConnect)
            SQLConnect.Open()
            adapterSCAC = New SqlDataAdapter(cmdSelSCAC)
            adapterSCAC.Fill(cmbSCACTable)
            cmbSCAC.DataSource = cmbSCACTable
            SQLConnect.Close()
            cmdSelSCAC.Dispose()
Jimbo99999

ASKER
Ok, I have the ComboBox loading with the following code.

However, I am unable to get the selected value from the combobox in code.

Dim ConnectionString As New SqlConnectionStringBuilder()
            With ConnectionString
                .DataSource = "(local)"
                .InitialCatalog = "Test"
                .IntegratedSecurity = True
            End With
            SQLConnect = New System.Data.SqlClient.SqlConnection(ConnectionString.ConnectionString())
            cmdSelSCAC = New SqlCommand("Select SCAC + ' - ' + [CARRIER NAME] as description " & _
                                                "from [CARRIER PROFILES] ", SQLConnect)
            SQLConnect.Open()
            adapterSCAC = New SqlDataAdapter(cmdSelSCAC)
            adapterSCAC.Fill(cmbSCACTable)
            With cmbSCAC
                .DataSource = cmbSCACTable
                .DisplayMember = "description"
                .SelectedIndex = 0
            End With
            SQLConnect.Close()
            cmdSelSCAC.Dispose()
Navneet Hegde

Hi!

OK I will just show you the sample I created.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Navneet Hegde

Hi!

You can get the SelectedText using

     Dim drView As DataRowView = DirectCast(ComboBox1.SelectedItem, DataRowView)
        Label1.Text = drView.Row.ItemArray(0).ToString()

Open in new window


And after this you can split with '-' to get your exact Value without description

Thanks!
Navneet Hegde

Hi!

Here is the UI

UI Image

And your code in here

   Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        '******************
        'Load SCAC ComboBox
        '******************
        Dim SQLConnect As New SqlConnection()
        Dim cmdSelSCAC As New SqlCommand()
        Dim adapterSCAC As New SqlDataAdapter()
        Dim cmbSCACTable As New DataTable()
        Dim ConnectionString As New SqlConnectionStringBuilder("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Northwind.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
        With ConnectionString
            .DataSource = ".\SQLEXPRESS"
            .AttachDBFilename = "|DataDirectory|\Northwind.mdf"
            .ConnectTimeout = 30
            .UserInstance = True
            .IntegratedSecurity = True
        End With
        SQLConnect = New System.Data.SqlClient.SqlConnection(ConnectionString.ConnectionString())
        cmdSelSCAC = New SqlCommand("Select CompanyName + '-' +  ContactName as description" & _
                                            " from [Customers] ", SQLConnect)
        SQLConnect.Open()
        adapterSCAC = New SqlDataAdapter(cmdSelSCAC)
        adapterSCAC.Fill(cmbSCACTable)

        With ComboBox1
            .DataSource = cmbSCACTable
            .DisplayMember = "description"
            .SelectedIndex = 0
        End With

        SQLConnect.Close()
        cmdSelSCAC.Dispose()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        Dim drView As DataRowView = DirectCast(ComboBox1.SelectedItem, DataRowView)
        Label1.Text = drView.Row.ItemArray(0).ToString()

    End Sub

Open in new window




Thanks!
Navneet Hegde

Hi!

You can further split to get only the requierd output by updating to below
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        Dim drView As DataRowView = DirectCast(ComboBox1.SelectedItem, DataRowView)
        Label1.Text = drView.Row.ItemArray(0).ToString().Split("-")(0)

    End Sub

Open in new window



Thanks!
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
adriankohws

It's difficult to manipulate if  you put ID and description in one combobox, then after selection, if you need just the ID to update, you need to reverse engineer again, kind of troublesome I felt.

I always use a normal combobox and a label to display by the side for description, making use of an "Arraylist"

You can easily not using databinding to combo box. Just fetch data from database and populate manually.

Form level: add an Arraylist
Private DescList As New ArrayList

 Sub LoadBrand()
        cbBrand.Items.Clear()
        Dim rd As SqlDataReader = Nothing
        Dim eCmd As New SqlCommand("SELECT brand, desc FROM BrandTable", cls.GetSqlcon)
        rd = eCmd.ExecuteReader
        Try
            While rd.Read
                cbBrand.Items.Add(rd("brand"))
                DescList.Add(rd("desc "))
            End While
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        Finally
            rd.Close()
            eCmd.Dispose()
        End Try
    End Sub

So having the above once the combo box index changed, identify the index and you get the description easily.

    Private Sub cbBrand_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbBrand.SelectedIndexChanged
        Dim x As Integer = cbBrand.SelectedIndex
        Yourlabel.text = DescList.Item(x).ToString()
    End Sub
Jimbo99999

ASKER
The idea to add the 2 columns of data in the query worked great. I then split off the front part of the selected text to do my add back to the database.

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