c# - 当前单元格不能设置为datagridview中的不可见单元格

标签 c# winforms

我在 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

"Current cell cannot be set to an invisible cell".

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

我在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);
  }
}

最佳答案

错误是不言自明的:您将 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);
        }
    }

关于c# - 当前单元格不能设置为datagridview中的不可见单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18018235/

相关文章:

css - windows 窗体使窗体从特定位置开始

c# - 为什么 LinqPad 在转储时对某些类型运行 ToString()?

c# - Entity Framework 集成测试无法运行多个单独通过的测试 [Resharper、NUnit、EF6]

c# - 构造函数注入(inject)对象的处置

c# - 在 UITypeEditor 的 EditValue 中访问额外的上下文数据

c# - 如何有效裁剪 *indexed* 位图并获得 *indexed* 结果?

c# - 将 RichTextBox.Text 数据绑定(bind)到字符串

c# - 使用 EPPlus 将 PrintArea 设置为 "Print Entire Workbook"

c# - .NET CQL 解析器是否存在?

c# - 如何衡量 MVC 网站的性能?