【问题标题】:Winforms DataGridView Cell Selection Change gives errorWinforms DataGridView 单元格选择更改给出错误
【发布时间】:2011-05-03 19:49:43
【问题描述】:

我在 Form_Activated 事件中有一个 winforms 屏幕和以下代码。

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: 指定的参数超出了 有效值的范围。参数名称: 列索引在 System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, 布尔值 setAnchorCellAddress,布尔值 验证CurrentCell,布尔值 通过鼠标单击)在 System.Windows.Forms.DataGridView.SetSelectedCellCoreInternal(Int32 columnIndex, Int32 rowIndex, 布尔值 选择)在 System.Windows.Forms.DataGridViewCell.set_Selected(布尔 值)

【问题讨论】:

  • genlGridIndex 的值是多少?

标签: winforms datagridview selection


【解决方案1】:

应该是

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

?

注意genlGrid1.Rows.Count &gt;= genlGridIndex + 1

【讨论】:

  • 我认为这不是问题的原因。当我们检查 genlGridIndex 为 0 或 1 时,它不会在 genlGridIndex 为 0 时执行 if 条件中的代码。所以它会在 genlGridIndex 为 1 时执行。当它为 0 时,它不会抛出异常,而是会不执行代码。还有其他一些事情是导致异常的原因
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多