• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 373
  • Last Modified:

Mulitple combobox columns

Desperately trying to figure this out??  I have ask this question before but atill having problems with it.

HOW DO I MAKE A COMBOBOX WITH MULITIPLE COLUMNS?

I have an access database, usinf visual studio.net.  I have a form with a combobox and lots of text fields,  the combobox is used to search for a particular record.  I need the combobox to hold the firstname lastname and ID columns of the datatable.

I am looking at creating datatable that will have one column with the ID and another with the last name and the firstname as a concatenated string.  I don't know how i go concatenating two datatable cloumns into one.  

Desperately needing help here.

Nerdy Girl

0
Nerdy_Girl88
Asked:
Nerdy_Girl88
3 Solutions
 
flavoCommented:
Hey neardy!

Example of mine

    Dim sSQL As String = "Select PreparerID, (LastName &", " & FirstName) as sName FROM tblPreparer order by [sname];"
            con = New OleDbConnection(Globals.conString)
            con.Open()
            'populate prepare combobox
            Dim cmdPreparer = New OleDbCommand(sSQL, con)
            Dim adptPreparer = New OleDbDataAdapter
            Dim dsPreparer = New DataSet
            adptPreparer.SelectCommand = cmdPreparer
            adptPreparer.Fill(dsPreparer, "tblPreparer")
            Me.cboPreparer.DataSource = dsPreparer.Tables("tblPreparer")
            Me.cboPreparer.DisplayMember = "sName"
            Me.cboPreparer.ValueMember = "PreparerID"
            'Cleanup
            dsPreparer.Dispose()
            adptPreparer.Dispose()
            cmdPreparer.Dispose()


The id is not shown, however can be retrived by using

Me.cboPreparer.selectedvalue

The name is generated like this

Select ID, (firstName & " " & LastName) as Name

                ^^^^^^   ^^^^   ^^^^^       ^^^^
               field            join         other field     alias

More help?? Im a cr@p teacher
0
 
RonaldBiemansCommented:
Another way is to add the column to your table something like

Lets say you have a table (dt) with the following fields

ID,Fname,Lname


        dt.Columns.Add("Names", GetType(System.String), "Fname + ' ' + Lname")

        ComboBox1.ValueMember = "ID"
        ComboBox1.DisplayMember = "Names"
        ComboBox1.DataSource = dt
0
 
RonaldBiemansCommented:
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
prakash_prkCommented:
Use a class to store your value
--------------------------------
Class details
    Private _id, _firstName, _lastName As String

    Public Sub New(ByVal id As String, ByVal lastname As String, ByVal firstname As String)
        Me._firstName = firstname
        Me._id = id
        Me._lastName = lastname
    End Sub

    Public Property ID() As String
        Get
            Return _id
        End Get
        Set(ByVal Value As String)
            _id = Value
        End Set
    End Property

    Public Property FirstName() As String
        Get
            Return _firstName
        End Get
        Set(ByVal Value As String)
            _firstName = Value
        End Set
    End Property

    Public Property LastName() As String
        Get
            Return _lastName
        End Get
        Set(ByVal Value As String)
            _lastName = Value
        End Set
    End Property

    Public Overrides Function toString() As String
        Return ID 'the property you want to display in combo box
    End Function
End Class
----------------------------------
use this code to add your object to combo box
Dim temp As New details("89", "gggg", "jsgdghg")
Me.ComboBox1.Items.Add(temp)
---------------------

The to string method is important your define the string you wnat to display in the combobox

Regards
Prakash
0
 
RacinRanCommented:
The bottom line is you can't have mutiple columns in the combo box.  So you store the ID in ValueMember property and concatenate whatever else you want into one column and put it in the DisplayMember property.


Racin
0
 
Nerdy_Girl88Author Commented:
Thank you all for your help....

Flavo
will your code create a datatable  with a column called sName. 'Which will be of string type holding the First name and last name.

 RonaldBiemans
That is exactly what i want to do.  

Just to confirm

 dt.Columns.Add("Names", GetType(System.String), "Fname + ' ' + Lname")

Fname and Lname are already columns in the dt table right?
And this will add a new column to that dt called Names.

Thanks
0
 
Nerdy_Girl88Author Commented:
RonaldBiemans

All the controls are bound to the same dataset,  I have a search combobox that uses the same datatable in the dataset,  If I use a different dataset to  fill the combobox how will my dataset know which record to display to the form.

Does theis make sense.
0
 
Nerdy_Girl88Author Commented:
Thnaks guys that works, however now my master detail form doesn't work.  It will only work for the master part.

I think it was because of the way i hace designed by master detail form.

Thanks you have help me with this problem I will post an new thread for the master detail problem.

Cheers
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now