Reputation: 1157
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?
Upvotes: 2
Views: 6900
Reputation: 2620
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.
Upvotes: 2