ComboBox Data Binding

Posted by Anilkumar431 under ADO.NET on 9/26/2015 | Points: 10 | Views : 349 | Status : [Member] | Replies : 2
Hi All,

I am doing a project in Windows forms application with the hellp of C# prog Lang. I got a small confusion when i am binding the data to ComboBox from Database.

Ex:
Image Id column is DisplayMember of ComboBox and the values like this 1 11 12 15 18 19 21.

But i need to display the ImageId's in the ComboBox as 1 2 3 4 5 6 etc....
Please any one help me out from this situation.


Thanks in Advance.


Anilkumar Madishetty




Responses

Posted by: Rajnilari2015 on: 9/26/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
2
Down
I presume you have the below initial setup
DataTable dt = GetData(); // Implement the function to retrieve data.

comboBox1.Datasource = dt; //set the combobox data source
comboBox1.DisplayMember = ImageID; // Display column
comboBox1.ValueMember = ImageData; // Value member column

And here is a simulated version of GetData()
public DataTable GetData()

{
DataTable dt = new DataTable();

//Add some columns
dt.Columns.Add(new DataColumn("ImageID", typeof(Int32)));
dt.Columns.Add(new DataColumn("ImageData", typeof(string)));

//Add some rows

//First Row
DataRow row1 = dt.NewRow();
row1("ImageID") = 1;
row1("ImageData") = "ImageData1";

//Second Row
DataRow row2 = dt.NewRow();
row2("ImageID") = 11;
row2("ImageData") = "ImageData11";

//Third Row
DataRow row3 = dt.NewRow();
row3("ImageID") = 12;
row3("ImageData") = "ImageData12";

//Fourth Row
DataRow row4 = dt.NewRow();
row4("ImageID") = 15;
row4("ImageData") = "ImageData15";

//Fifth Row
DataRow row5 = dt.NewRow();
row5("ImageID") = 18;
row5("ImageData") = "ImageData18";

//Sixth Row
DataRow row6 = dt.NewRow();
row6("ImageID") = 19;
row6("ImageData") = "ImageData19";

//Seventh Row
DataRow row7 = dt.NewRow();
row7("ImageID") = 21;
row7("ImageData") = "ImageData21";

//Add the rows to the table
dt.Rows.Add(row1);
dt.Rows.Add(row2);
dt.Rows.Add(row3);
dt.Rows.Add(row4);
dt.Rows.Add(row5);
dt.Rows.Add(row6);
dt.Rows.Add(row7);

//return the table
return dt;
}

Now you have a new requirement so that the ImageID should be in sequential order. Right? If so, here is an attempt.
Add a new column to the existing datatable as under
public DataTable GetModifiedTable(DataTable sourceDT)

{
DataTable destinationDT = sourceDT;
DataColumn newCol = new DataColumn("ModifiedImageID", typeof(Int32));
destinationDT.Columns.Add(newCol);
int counter = 1;
foreach (DataRow row in destinationDT.Rows)
{
row["ModifiedImageID"] = counter;
counter++;
}
return destinationDT;
}

And change the below
comboBox1.DisplayMember = ModifiedImageID;

So the final modified version should be
DataTable dt = GetModifiedTable(GetData()); // Implement the function to retrieve data.

comboBox1.Datasource = dt; //set the combobox data source
comboBox1.DisplayMember = ModifiedImageID; // Display column
comboBox1.ValueMember = ImageData; // Value member column

Hope this helps.

--
Thanks & Regards,
RNA Team

Anilkumar431, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 9/27/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
1
Down
Another way

var dt = GetData();

BindingSource bs = new BindingSource();
var lstDS = dt
.AsEnumerable()
.Select(dr=>
ModifiedImageID = Enumerable.Range(1,dt.Rows.Count)
,ImageData = dr.Field<string>("ImageData"))
.ToList();
bs.DataSource = lstDS;
comboBox1.DataSource = bs.DataSource;
comboBox1.DisplayMember = "ModifiedImageID";
comboBox1.ValueMember = "ImageData";


--
Thanks & Regards,
RNA Team

Anilkumar431, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response