2012-10-05 76 views
0

這是我在dgv2Cb上拋出異常的代碼。
有人可以告訴我我要去哪裏嗎?將組合框添加到datagridviewcell投擲索引超出範圍例外

Dim dgv2Cb As New DataGridViewComboBoxCell 

Dim str1 = "Port" 
Dim rowIndex = 0 

If str1 = "Port" Then 
    dgv2Cb.Items.Add("1") 
    dgv2Cb.Items.Add("2") 

    dgv2.Rows.Add(str1) 
    dgv2.Rows(rowIndex).Cells(1) = dgv2Cb 'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 
End If 

回答

0

錯誤是與.Cells(1)代碼,我敢冒險猜測。

您是否期待.Cells(1)返回"2"Cells引用表中的列,而不是您的ComboBox中的值。

你會發現這個例子的代碼有幫助:
http://msdn.microsoft.com/en-us/library/ms404353%28v=vs.100%29.aspx

編輯

在你的代碼的第二次審查,它看起來像你正試圖將列添加到DataGridView?在這種情況下,您應該使用:

dgv2.Columns.Add(dgv2Cb)