Display two fields in combobox

OMRebel

Active member
Joined
Sep 27, 2006
Messages
44
Programming Experience
Beginner
Hey everyone. I have a combox that I want to display two fields in. For instance, I have the following:

VB.NET:
        con.Open()
        sql = "Select * From Tech"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "Tech")
        con.Close()

        cboTech.DataSource = ds.Tables("Tech").DefaultView
        cboTech.DisplayMember = "LastName"
        cboTech.ValueMember = "LastName"
        cboTech.SelectedIndex = -1

Right now, it is displaying the LastName. I also want it to display a field called FirstName. Can someone point me in the direction on how to do that?

Thanks.
 
The standard comboBox can only display one field. There are third party and free custom controls that can show 2 or more fields (look on codeproject.com for example).
To use the standard combobox, you could create a SQL statement that combines the two fields you want to display. In my example below I have included an ID field which is the Primary Key in the table; hopefully you have a primary key in the table Tech as there may be duplicates of the LastName field and it wouldn't be a good idea to use the LastName as the valuemember.
VB.NET:
sql = "SELECT ID, FirstName + N' ' + LastName AS Name FROM Tech"
'...
cboTech.DisplayMember = "Name"
cboTech.ValueMember = "ID"
+ N' ' + in the code concatenates FirstName and LastName with a space in between.
 
Hmmmm...just tried it and got:
Syntax error (missing operator) in query expression 'FirstName + N' ' + LastName'.
 
The SQL statement I supplied is for SQL server. I didn't see the OleDb.
Remove the N and it should work.

Remember when asking database questions that it's always a good idea to specify what type of database you're using; you'll get more specific help that way.
 
Back
Top