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

Jimbo99999
Jimbo99999 used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Navneet.Net Full Stack Developer

Commented:
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!

Author

Commented:
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
.Net Full Stack Developer
Commented:
Hi!

It would be better if you could share your code.

However you can query prepare query itself like
SELECT textColuumnName + DescColumnName AS ComboOutput
FROM TableName

OR

Bind the Two Column to the Combo TextField


Thanks!
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Author

Commented:
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

Author

Commented:
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()

Author

Commented:
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.Net Full Stack Developer

Commented:
Hi!

OK I will just show you the sample I created.
Navneet.Net Full Stack Developer

Commented:
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.Net Full Stack Developer

Commented:
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.Net Full Stack Developer

Commented:
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!
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

Author

Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial