【问题标题】:Current cell cannot be set to an invisible cell in datagridview当前单元格不能设置为 datagridview 中的不可见单元格
【发布时间】:2013-08-02 13:34:27
【问题描述】:

我在 datagridview 中遇到问题。 I have done some code in keydown event for changing the tab focus but when tab reaches last of column it gives a error

“当前单元格不能设置为不可见单元格”。

我使最后一个单元格不可见,因为我不想让那个单元格可见。

我在 KeyDown 事件中编写了以下代码

private void m3dgvDepositDetails_KeyDown(object sender, KeyEventArgs e)
{
  try
  {
    if (e.KeyCode == Keys.Tab && notlastColumn)
    {
      e.SuppressKeyPress = true;
      int iColumn = m3dgvDepositDetails.CurrentCell.ColumnIndex;
      int iRow = m3dgvDepositDetails.CurrentCell.RowIndex;
      if (iColumn == m3dgvDepositDetails.Columns.Count - 1)
        m3dgvDepositDetails.CurrentCell = m3dgvDepositDetails[0, iRow + 1];
      else
        m3dgvDepositDetails.CurrentCell = m3dgvDepositDetails[iColumn + 1, iRow];
    }
  }
  catch (Exception ex)
  {
    CusException cex = new CusException(ex);
    cex.Show(MessageBoxIcon.Error);
  }
}

【问题讨论】:

  • 在最后一个可见列之后,您的网格中是否有任何隐藏列?
  • 我使最后一个单元格不可见,因为我不想让那个单元格可见。
  • 所以你应该使用if (iColumn >= m3dgvDepositDetails.Columns.Count - 2)

标签: c# winforms


【解决方案1】:

错误很明显:您将CurrentCell 设置为不可见单元格并且被禁止,这意味着单元格的行或单元格的列被隐藏。为避免这种情况,请不要隐藏行/列或在设置 CurrentCell 之前检查 Visible 属性。

如果问题是您应该使用的最后一列:

private void m3dgvDepositDetails_KeyDown(object sender, KeyEventArgs e)
    {
        try
        {
            if (e.KeyCode == Keys.Tab && notlastColumn)
            {
                e.SuppressKeyPress = true;
                int iColumn = m3dgvDepositDetails.CurrentCell.ColumnIndex;
                int iRow = m3dgvDepositDetails.CurrentCell.RowIndex;
                if (iColumn >= m3dgvDepositDetails.Columns.Count - 2)
                    m3dgvDepositDetails.CurrentCell = m3dgvDepositDetails[0, iRow + 1];
                else
                    m3dgvDepositDetails.CurrentCell = m3dgvDepositDetails[iColumn + 1, iRow];

            }
        }
        catch (Exception ex)
        {
            CusException cex = new CusException(ex);
            cex.Show(MessageBoxIcon.Error);
        }
    }

【讨论】:

    【解决方案2】:

    当您尝试选择隐藏单元格时会发生此错误。此外,您不应在 datagridview 中将行设置为不可见,因为它有错误。

    一种解决方案不是将行设置为不可见,而是过滤数据源并仅获取您想要的那些记录。这会很慢,但可以作为一种解决方法。

    或

    您可以尝试使用以下(未测试)

    cm.SuspendBinding();
    dataGridView1.Rows[0].Visible = false; // Set your datatgridview invisible here
    cm.ResumeBinding();
    

    【讨论】:

      猜你喜欢
      • 2014-09-10
      • 2020-02-15
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 2013-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多