I've been stuck with this since yesterday and I can't find any solution to my problem. I am busy with a WinForms App which has DataGridView on it. I have a List<> object which I want to bind to it.
The DataGrid has 3 normal columns and then it also has a DataGridViewComboBoxColumn.
My List<> object has 3 string fields and then a Arraylist field.
Each ArrayList in the List<> object has different data that corresponds with the rest of the single object within the List<>.
So in other words, I have something like this:
|
Now if I bind the List<> to the DataGrid then all the strings will bind fine, but I can't find a way to bind the Arraylist to the ComboBoxColumn. In ASP.NET this is much easier as you can use the RowDataBind event to bind the ArrayList to the Dropdownbox being created for each row. But I can't do it in WinForms. I don't get how something that should be much simpler can so complicated.
Any help would be greatly appreciated.
Thanks
NeCroFirePosted Oct 5, 2010, 10:20 AM
I don't understand this. I've tried everything I can think of including what was posted here and nothing works. It renders the Combobox, but nothing happens when you click on the Comboboxes.
It seems to work if I have no other columns in my grid and only run the code posted by Rakesh. But try anything else and it stops working.
EDIT:
Ok, I found my problem. I had the grid set to ReadOnly. I changed it to false now and the dropdowns work fine. That's what happens when you copy a Grid from one form to another because you don't want to change all the default settings again...
Thanks
Rakesh JhaPosted Oct 2, 2010, 1:03 AM
Below code will solve your problem but you have to handle DataError event
this.dataGridView1.DataSource = Obj;
DataGridViewComboBoxColumn ObjDGC4 = new DataGridViewComboBoxColumn();
ObjDGC4.HeaderText = "Field4";
ObjDGC4.Name = "Field4";
this.dataGridView1.Columns.Add(ObjDGC4);
int iRowCntr=0;
foreach (_MyObj ListObj in Obj)
{
DataGridViewComboBoxCell ObjCBCell = new DataGridViewComboBoxCell();
ObjCBCell.DataSource = ObjMy.Field4;
this.dataGridView1.Rows[iRowCntr].Cells[0] = ObjCBCell;
iRowCntr++;
}