Click here to Skip to main content
15,912,897 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have 1 combobox and 2listbox and i need to make items in combobox show in the listboxes list one for name and list 2 for price or number
plz Please i need help
Posted
Updated 5-May-12 11:21am
v2
Comments
Shahin Khorshidnia 5-May-12 17:20pm    
I didn't understand your intention.
Dev-0001 6-May-12 2:48am    
Not Clear .

1 solution

If you are using Windows Forms then the following procedure can be used to get the above result.
1. Create a DataTable as shown below using Expression for the NameAndPrice column.
C#
DataTable Persons = new DataTable();
Persons.Columns.Add("Name",typeof(string),null);
Persons.Columns.Add("Price",typeof(decimal),null);
Persons.Columns.Add("NameAndPrice",typeof(string),"Name + ': ' + Price");
//Add sample data
Persons.Rows.Add("John",45.45);
Persons.Rows.Add("Mehul",54.34);

2. Then assign the Persons DataTable to the DataSource property of ComboBox and ListBoxes and set the ValueMember and DisplayMember properties as shown below:

C#
listBox1.DataSource = Persons;
listBox1.ValueMember= "Name";
listBox1.DisplayMember="Name";

C#
comboBox1.DataSource = Persons;
comboBox1.ValueMember= "Name";
comboBox1.DisplayMember= "NameAndPrice";

C#
listBox2.DataSource = Persons;
listBox2.ValueMember= "Name";
listBox2.DisplayMember="Price";
 
Share this answer
 
Comments
Maciej Los 11-May-12 2:27am    
Good answer, my 5!
VJ Reddy 11-May-12 3:22am    
Thank you, losmac.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900