Suppose we have a Data-table as under
public DataTable GetData()
{
DataTable dt = new DataTable();
//Add some columns
dt.Columns.Add(new DataColumn("ID", typeof(Int32)));
dt.Columns.Add(new DataColumn("Data", typeof(string)));
//Add some rows
for(int i=1;i<=10;i++)
{
DataRow dr = dt.NewRow();
dr("ID") = i;
dr("Data") = string.Concat("Data",i);
dt.Rows.Add(row); //add rows to table
}
//return the table
return dt;
}
Now we have to convert the Data-Table to a Generic LIST
var dt = GetData();
var lstDS = dt .AsEnumerable();
The next thing is to add a new column to the LIST with sequential values. For this to happen, the
Enumerable.Range will serve the purpose.
var dt = GetData();
var lstDS = dt
.AsEnumerable()
.Select(dr=>
SequentialID = Enumerable.Range(1,dt.Rows.Count)
,ID = dr.Field<string>("ID")
,Data = dr.Field<string>("ImageData"))
.ToList();
Now use the Binding Source(
https://msdn.microsoft.com/en-us/library/aa480734.aspx ).The BindingSource component provides currency management, change notification and the ability to easily access the members in a bound list.
BindingSource bs = new BindingSource();
bs.DataSource = lstDS;
Finally, populate the ComboBox
comboBox1.DataSource = bs.DataSource;
comboBox1.DisplayMember = "SequentialID";
comboBox1.ValueMember = "Data";
The complete piece of code will be
BindingSource bs = new BindingSource();
var dt = GetData();
var lstDS = dt
.AsEnumerable()
.Select(dr=>
SequentialID = Enumerable.Range(1,dt.Rows.Count)
,ID = dr.Field<string>("ID")
,Data = dr.Field<string>("ImageData"))
.ToList();
bs.DataSource = lstDS;
comboBox1.DataSource = bs.DataSource;
comboBox1.DisplayMember = "SequentialID";
comboBox1.ValueMember = "Data";