Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top