How to display multiple values in a combo box from a datatable

Posted by Niladri.Biswas under C# category on | Points: 40 | Views : 1404
I have a datatable having 3 fields out of which I need to combine the values of the 2nd and 3rd field and display in the combo box.

DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Col1", typeof(string));
dt.Columns.Add("Col2", typeof(string));
dt.Columns.Add("ConcatenatedField", typeof(string), "Col1 + ' : ' +Col2");

Enumerable.Range(1, 10).ToList().ForEach(i => dt.Rows.Add(i, string.Concat("Col1", i), string.Concat("Col2", i)));

comboBox1.DataSource = dt;
comboBox1.DisplayMember = "ConcatenatedField";

Comments or Responses

Login to post response