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.
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");
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:
listBox1.DataSource = Persons;
listBox1.ValueMember= "Name";
listBox1.DisplayMember="Name";
comboBox1.DataSource = Persons;
comboBox1.ValueMember= "Name";
comboBox1.DisplayMember= "NameAndPrice";
listBox2.DataSource = Persons;
listBox2.ValueMember= "Name";
listBox2.DisplayMember="Price";