-
September 19th, 2011, 12:18 PM
#1
Show multiple column from database in a combobox
I am a newbie in C#. How can I show two columns from a database in a DropDownList (ComboBox)?
Currently I can show one column from my database.
cbBox1.DataSource = dba.generateNameList();
cbBox1.DisplayMember = "Name";
/* Here dba.generateNameList() returns an ArrayList which holds the data of my database
This way I could show one column in my cbBox1 successfully.
*/
I am trying to show "ID" from my database along with "Name" Like this "Johny(1001)"
I tried it this ways
cbBox1.DisplayMember = "Name" + "(ID)";
But, it shows compiler error.
Somebody please help me.
-
September 19th, 2011, 02:20 PM
#2
Re: Show multiple column from database in a combobox
Not sure if the itemdata property is still on the combobox in .net framework or not. In VB6 I would set the itemdata property to the ID number [numeric only] of course the id wouldbe hidden in that case.
You can of course concantonate the name and ID together if you want them both displayed. Based on what you have given though I can't really tell you want you would need to do. I can tell you that the () around ID inside quotes will not work and in the case of displaymemeber it is looking for the name of the member to display if the case above it would try for a memeber named Name(ID) which most likely does not exist.
You can create a sub which will read the records from the db cocantonate the name field + "(" + id field +")" and use the additem method to add the newly created string to your combo box to get the display you seek.
Always use [code][/code] tags when posting code.
-
September 22nd, 2011, 07:50 AM
#3
Re: Show multiple column from database in a combobox
DataMiser,
Thanks for your reply.
I successfully did it in another way.
-
September 22nd, 2011, 12:54 PM
#4
Re: Show multiple column from database in a combobox
If you are using WPF, you can style the combobox template to add columns to the dropdown. That way everything will appear nicely formatted.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|