1

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?

joaocarlospf
  • 1,129
  • 2
  • 13
  • 23
  • 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 http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcell.aspx – MethodMan Feb 13 '12 at 15:54
  • Here is another helpful link that will explain it even more .. http://homepage.ntlworld.com/herring1/datagrid.html – MethodMan Feb 13 '12 at 15:59
  • 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 Feb 13 '12 at 17:31
  • 1
    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 Feb 13 '12 at 17:44
  • Why would I need to create a new instance of DataGridViewComboBoxColumn? – joaocarlospf Feb 13 '12 at 18:37
  • I am afraid is not possible to set the items of the dataGrid Comboboxes like I want. This is the third time I have this problem. The other times I found ways to circumvent the problem, but this time i need to solve it anyway. =/ – joaocarlospf Feb 13 '12 at 18:42

1 Answers1

1

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.

Community
  • 1
  • 1
Chuck Wilbur
  • 2,510
  • 3
  • 26
  • 35
  • PERFECT! The code was on the constructor.. when I moved to the OnLoad method it worked perfectly! That solved my problem very well. Thanks a lot. – joaocarlospf Feb 13 '12 at 23:24
  • I just don't understand why is not possible to fill a combobox cell in the constructor.. – joaocarlospf Feb 13 '12 at 23:25
  • The WinForms C# controls are just wrappers over old the Windows C API calls with their flags and messages and craziness. There's all kinds of stuff happening under the hood between construction and load, and one of those things is probably the construction of the container for the combo box choices. Before that's constructed the call to set the choices (which probably gets dispatched as a message with params and stuff) just gets ignored by the underlying API. This has been a half-assed explanation based on foggy memories of the Windows API and should not be used in production code ;) – Chuck Wilbur Feb 14 '12 at 14:53
  • Yes, filling in the DataGridViewComboBoxCell in the Load event works. But, don't forget to set the DataSource, DisplayMember and ValueMember: `cellcb.DataSource = lstChoices; cellcb.DisplayMember = "CfgChoiceComboString"; cellcb.ValueMember = "CfgChoiceComboPos";` – Jim Lahman Nov 05 '12 at 14:26