0

I am trying to populate a datagridview combobox column with comboboxcells in winforms where each row has a different collection derived from the nested list within the dictionary, the dictionary works fine when i iterate over it and get its objects and their string values, however every different combination i have exhausted to populate the combobox cells on form load has failed. Is it possible? I have found other posts where they use cellclick events etc. I prefer to populate on form initialization.

//this works
public void create datatable()
{
    DataGridViewComboBoxColumn Data_CmBx_Col_ObjectType = new  DataGridViewComboBoxColumn();
    Data_CmBx_Col_FamilyType.Name = _ADD_OBJECT_TYPE;
    Data_CmBx_Col_FamilyType.HeaderText = _ADD_OBJECT_TYPE;
    dataGridView.Columns.Insert(6, Data_CmBx_Col_ObjectType);


    //pop combobox, the dictionary works
    int i = 0;
    foreach (KeyValuePair<object, List<objectType>> objectAndType in combined_Dictionary)
    {
        i++;
        if (rowIndex <= combined_Dictionary.Count)
        {                 
            CreateCustomComboBoxDataSouce(i, objectAndType.Value);                 
        }
    }

    //Bind dataGridView to a datatable
    dataGridView_Family.DataSource = data;
}//end method

//method is called and fails with index out of range error collection
private void CreateCustomComboBoxDataSouce(int row, List<objectAndType> type) //row index ,and two parameters
{
    DataGridViewComboBoxCell comboCell = dataGridView_objectAndType[6, row] as DataGridViewComboBoxCell;
    comboCell.DataSource = new BindingSource(type, null);
}//end method
Mohammad Akbari
  • 4,486
  • 6
  • 43
  • 74

1 Answers1

0

The index is zero based, so it must be strictly less than its count

if (rowIndex < combined_Dictionary.Count) // not <= but without =
  • Thanx, however after removing the = sign it makes no difference to the error result. I have even tried setting the max row index to two and nothing happens. DataGridViewComboBoxCell comboCell = dataGridView_objectAndType[6, row] as DataGridViewComboBoxCell; This line is what causes the issue though so I thought it might have something to do with the index as well. I can remove the line of code and create empty combobox columns easily enough – user7773578 Apr 02 '17 at 07:52
  • "*however after removing the = sign it makes no difference to the error result.*" you simply don't understand the basic of C# programming. Try to learn the elementary concepts and carefully read the suggestions you receive before writing other nonsense here –  Apr 02 '17 at 08:00
  • However it still isn't relevant to the problem. I have tried your solution and it doesn't work I'm sorry. – user7773578 Apr 02 '17 at 08:52