2011-05-03 37 views
1

我有一個winforms屏幕和Form_Activated事件中的下面的代碼。Winforms DataGridView單元格選擇更改提供了錯誤

if (genlGrid1.Rows.Count > 0 && genlGrid1.Rows.Count <= genlGridIndex + 1 && (genlGridIndex >= 0 && genlGridIndex < 2))  
{  
    //Looks like below line is where exception occurs  
    genlGrid1.Rows[genlGridIndex].Cells[0].Selected = true;  
} 

我非常肯定當這行代碼執行時,網格有2行9個單元。

當這行代碼執行時,我得到下面的錯誤。不知道電網正在發生什麼。什麼是SetCurrentCellAddressCore,爲什麼我們得到這個異常?

有人可以幫忙嗎?

System.ArgumentOutOfRangeException: 指定參數超出所述 有效值範圍的。參數名: columnIndex在 System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(的Int32 columnIndex,的Int32的rowIndex,布爾 setAnchorCellAddress,布爾 validateCurrentCell,布爾 throughMouseClick)在 System.Windows.Forms.DataGridView.SetSelectedCellCoreInternal(的Int32 columnIndex,的Int32 rowIndex位置,布爾 選擇的)在 System.Windows.Forms.DataGridViewCell.set_Selected(布爾 值)

+0

什麼是genlGridIndex的價值? – 2011-05-04 03:39:33

回答

0

它應該是

if (genlGrid1.Rows.Count > 0 && 
    genlGrid1.Rows.Count >= genlGridIndex + 1 && 
    (genlGridIndex >= 0 && genlGridIndex < 2))  
{   
    genlGrid1.Rows[genlGridIndex].Cells[0].Selected = true;  
} 

genlGrid1.Rows.Count >= genlGridIndex + 1

+0

我不認爲這是問題的原因。當我們檢查genlGridIndex是0還是1時,當genlGridIndex爲0時,它不會執行if條件中的代碼,因此它會在genlGridIndex爲1時執行。當它爲0時,它不會拋出異常,而是它會不執行代碼。還有一些其他的東西是由外部原因造成的 – 2011-05-05 05:04:17

相關問題