【问题标题】:DataGridComboBoxColumn Values Different For each Row每行的 DataGridComboBoxColumn 值不同
【发布时间】:2017-08-27 04:28:46
【问题描述】:

我正在尝试用 winforms 中的组合框单元填充 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

【问题讨论】:

  • 注意:它在创建组合框单元的行上出错

标签: c# winforms indexoutofboundsexception datagridcomboboxcolumn datagridviewcomboboxcell


【解决方案1】:

索引是从零开始的,所以它必须严格小于它的计数

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

【讨论】:

  • Thanx,但是在删除 = 符号后,它对错误结果没有影响。我什至尝试将最大行索引设置为 2,但没有任何反应。 DataGridViewComboBoxCell comboCell = dataGridView_objectAndType[6, row] as DataGridViewComboBoxCell;这条线是导致问题的原因,所以我认为它也可能与索引有关。我可以很容易地删除这行代码并创建空的组合框列
  • "但是去掉 = 符号后,错误结果没有任何影响。" 你根本不了解 C# 编程的基础。尝试学习基本概念并仔细阅读您收到的建议,然后再在这里写其他废话
  • 但是它仍然与问题无关。我已经尝试了您的解决方案,但它不起作用我很抱歉。
猜你喜欢
  • 2017-01-16
  • 1970-01-01
  • 1970-01-01
  • 2012-01-23
  • 2019-11-27
  • 2015-01-27
  • 1970-01-01
  • 1970-01-01
  • 2021-03-28
相关资源
最近更新 更多