DataGridComboBoxColumn值对于每一行都不同

时间:2017-04-02 06:50:03

标签: c# winforms indexoutofboundsexception datagridcomboboxcolumn datagridviewcomboboxcell

我正在尝试使用winforms中的comboboxcells填充datagridview组合框列,其中每一行都有一个从字典中的嵌套列表派生的不同集合,当我迭代它并获取其对象及其字符串值时,字典工作正常,但是,我在表单加载时填充组合框单元的每个不同组合都失败了。可能吗?我找到了其他帖子,他们使用cellclick事件等。我更喜欢填写表单初始化。

//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

1 个答案:

答案 0 :(得分:0)

索引零基于,因此它必须严格小于其计数

if (rowIndex < combined_Dictionary.Count) // not <= but without =