0
votes

I want to set the items of each DataGridViewComboBoxCell individually (beacause each combobox must have different items) in my DataGridView. I use this code to set the items:

foreach (DataGridViewRow row in grid.Rows)
{
    ((DataGridViewComboBoxCell)row.Cells[1]).Items.Clear();
    foreach (Product prod in _ProductList)
    {
        ((DataGridViewComboBoxCell)row.Cells[1]).Items.Add(prod.Name);
    }
}

Debugging I see the items of the DataGridViewComboBoxCell is correctly set, but when I look at the grid, the combos are empty.

Making different tests I realized that if I set items after the form is loaded (in a click event for example) the items are shown normally.

What should I do to load the items at form load time?

1
does the DataGridViewCombox have a DataSource attached to it.. I wonder if setting the DataSource for that particular item to string.Empty would fix your issue.. sounds like a DataBinding Issue look at this link to possibly select Alternate DataSource then set the items that you are trying to set via the foreach loop you have msdn.microsoft.com/en-us/library/… - MethodMan
Here is another helpful link that will explain it even more .. homepage.ntlworld.com/herring1/datagrid.html - MethodMan
Alternatively I tried to use DataSource instead of setting the items in a while looop. But the problem remains the same. If I set the DataGridViewComboBoxColumn instead DataGridViewComboBoxCell the the items are correctly filled in the grid, but this solution is not possible for me because each cell have different items.. - joaocarlospf
sounds like this is possible but what you would have to do is create a new instance of that DataGridviewCoboBoxColumn.. does this make sense..?? - MethodMan
Why would I need to create a new instance of DataGridViewComboBoxColumn? - joaocarlospf

1 Answers

1
votes

In what function are you running your foreach loop? If in the constructor, that may be too early. Try moving it to Form_Load or another handler that runs later.

I answered a question about setting the current value of the combo box in a column here, and you might be having a similar problem. I know setting the cells' DataSource works if you do it late enough in the life cycle of the control, because I did it here.