PDA

Click to See Complete Forum and Search --> : combo box, 2 columns [*Resolved *]


steve_rm
Jun 12th, 2005, 10:52 AM
Hello,

I have a database table with 2 fields, DepartmentCode, and DepartmentName. I want to add the values of these 2 fields into a combo box from the database. I can add one field, but how can you add 2 fields to the combox so that when the user clicks it, it will display all the departmentCode and Department name in 2 columns.

I can do this with just one field, but l am having trouble tying to display the extra field. Is this possible in a combox box?

Thanks in advance,

Steve

Hack
Jun 13th, 2005, 07:29 AM
Concantanate them using the & character.

mar_zim
Jun 13th, 2005, 08:19 PM
as what hack said you can use & or the + sign.

'like this one
combobox1.items.add("field1" + " " + "field2")

jmcilhinney
Jun 13th, 2005, 11:19 PM
There are multi-column ComboBoxes available on the Net. Try the code bank for this site and if there isn't one then look elsewhere. The Code Project is always a good place to start.

Andy
Jun 15th, 2005, 05:50 PM
Concantanate them using the & character.
you can't use the & concat method in c#. you have to use +
You really should be concating them at the database anyway...less load on your app . like this:
SELECT field1 + ' ' + field2 AS BothFields FROM TABLE
then, when you bind (i'm assuming you're binding), bind to 'BothFields'

:wave:

steve_rm
Jun 17th, 2005, 06:33 AM
Thanks everyone,

Problem solved.

Andy
Jun 17th, 2005, 11:39 PM
Any insight on how you solved it? Someone else reading this thread may be interested as well.

steve_rm
Jun 19th, 2005, 12:23 AM
Hello,

Here is the full solution to the problem. Thanks for you help.

//Load the combo box of the department names
cmd.CommandText = "SELECT DepartmentCode + ' ' + DepartmentName AS CodeAndName FROM Department";
OleDbDataReader dReaderDepartments = cmd.ExecuteReader();

while ( dReaderDepartments.Read() )//While there are departments to add to the combo box
{ //Add each item from the data reader to the combo box
cboDepartment.Items.Add( dReaderDepartments.GetString(0) );
cboEmployeeDepartments.Items.Add ( dReaderDepartments.GetString(0) );
}
dReaderDepartments.Close();
cmd.Dispose();