0

我想在winforms中填充一個datacombination組合框的組合框,其中每行都有一個不同的集合,從字典中的嵌套列表派生而來,當我遍歷它並獲取其字典對象和它們的字符串值,但是每個不同的組合我已用盡填充表單加載中的組合框單元失敗。可能嗎?我發現其他職位,他們使用cellclick事件等。我更喜歡填寫表單初始化。DataGridComboBoxColumn值不同對於每一行

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

注:錯誤在哪裏的comboboxcell創建 – user7773578

回答

0

該指數從零開始,所以必須嚴格小於其計數

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

感謝名單線,但除去後=簽名對錯誤結果沒有任何影響。我甚至嘗試將最大行索引設置爲2,並沒有任何反應。 DataGridViewComboBoxCell comboCell = dataGridView_objectAndType [6,row] as DataGridViewComboBoxCell;這條線是導致這個問題的原因,所以我認爲它也可能與索引有關。我可以刪除代碼行,並且很容易創建空的組合框 – user7773578

+0

「*然而,在刪除=符號之後,它對錯誤結果沒有任何影響。*」您根本不瞭解C#編程的基本知識。嘗試學習基本概念並仔細閱讀您在收到其他廢話之前所收到的建議 – 2017-04-02 08:00:11

+0

但是它仍然與問題無關。我已經嘗試了您的解決方案,但無法正常工作,我很抱歉。 – user7773578

相關問題