【问题标题】:Tab to the next cell in a DataGridView跳到 DataGridView 中的下一个单元格
【发布时间】:2012-08-12 21:45:10
【问题描述】:

我有一个 DataGridView,其中有许多单元格的 ReadOnly 属性设置为 True。

当用户使用 tab 键在单元格中切换时,如果 ReadOnly 属性为 true,我想将焦点移动到下一个单元格上。我的代码如下:

    private void filterGrid_CellEnter(object sender, DataGridViewCellEventArgs e)
    {
        if (!filterGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].ReadOnly)
        {
            EditCell(sender, e);                
        }
        else
        {
            //Move to the next cell
            filterGrid.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Selected = true;
        }            
    }

但是,当我运行上述代码时,我收到以下错误:

操作无效,因为它会导致对 SetCurrentCellAddressCore 函数的可重入调用。

我使用的是 C# 4.0

提前致谢。

【问题讨论】:

  • 你可能想先做一些用户测试。这在数据输入过程中可能会非常令人不安。
  • 一旦我得到这个工作,我会的!

标签: c# datagridview


【解决方案1】:

您可以使用 datagridview 单元格 Enter 事件并绕过只读单元格选项卡索引到另一个单元格。这是我的例子:-

    private void dgVData_CellEnter(object sender, DataGridViewCellEventArgs e)
    {
        if (dgVData.CurrentRow.Cells[e.ColumnIndex].ReadOnly)
        {
            SendKeys.Send("{tab}");
        }
    }

【讨论】:

    【解决方案2】:

    我对这样的东西使用派生的 DataGridView,这只会影响 Tab 键,因此用户仍然可以单击只读单元格进行复制粘贴等。

    using System.Windows.Forms;
    
    namespace WindowsFormsApplication5
    {
        class MyDGV : DataGridView
        {
            public bool SelectNextCell()
            {
                int row = CurrentCell.RowIndex;
                int column = CurrentCell.ColumnIndex;
                DataGridViewCell startingCell = CurrentCell;
    
                do
                {
                    column++;
                    if (column == Columns.Count)
                    {
                        column = 0;
                        row++;
                    }
                    if (row == Rows.Count)
                        row = 0;
                } while (this[column, row].ReadOnly == true && this[column, row] != startingCell);
    
                if (this[column, row] == startingCell)
                    return false;
                CurrentCell = this[column, row];
                return true;
            }
    
            protected override bool ProcessDataGridViewKey(KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Tab)
                    return SelectNextCell();
                return base.ProcessDataGridViewKey(e);
            }
    
            protected override bool ProcessDialogKey(Keys keyData)
            {
                if ((keyData & Keys.KeyCode) == Keys.Tab)
                    return SelectNextCell();
                return base.ProcessDialogKey(keyData);
            } 
        }
    }
    

    【讨论】:

      【解决方案3】:

      这两个建议中的一个应该可以工作

      使用

       else
              {
                  filterGrid.ClearSelection();
                  filterGrid.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Selected = true;
              }
      

      Otherwise one other method is suggested here along with the reason
      这也意味着同样的问题:-
      InvalidOperationException - When ending editing a cell & moving to another cell

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-03
        相关资源
        最近更新 更多