【问题标题】:How to use enter as a tab in datagridview如何在datagridview中使用enter作为选项卡
【发布时间】:2019-09-11 13:18:49
【问题描述】:

我想在 datagridview 中使用 enter 键作为 Tab 键的替代键 我有四列,当我输入第三列时,它应该通过按 Enter 移动到同一行的第四列,它移动到下一行而不是下一列。 我正在使用以下代码

if (e.KeyData == Keys.Enter)
{
    int col = dataGridView1.CurrentCell.ColumnIndex;
    int row = dataGridView1.CurrentCell.RowIndex;

    if (col < dataGridView1.ColumnCount - 1)
    {
        col++;
    }
    else
    {
        col = 0;
        row++;
    }
    if(row == dataGridView1.RowCount)
        dataGridView1.Rows.Add();


    dataGridView1.CurrentCell = dataGridView1[col, row];
    e.Handled = true;
}

【问题讨论】:

  • SendKeys.Send("{TAB}");SendKeys.Send("\t"); 有效吗?
  • 不,这是行不通的
  • 如果你尝试把if (col &lt;= dataGridView1.ColumnCount - 1) ?
  • 同一问题关注下一行而不是下一列
  • 代码似乎有效。问题是什么?你在用什么活动?

标签: c#


【解决方案1】:

此代码运行正常。 应该是 false 允许用户添加行。

   if (e.KeyCode == Keys.Enter)
            {
                int col = dataGridView1.CurrentCell.ColumnIndex;
                int row = dataGridView1.CurrentCell.RowIndex;

                if (row == dataGridView1.Rows.Count - 1 && col < dataGridView1.Columns.Count - 1)
                {
                    dataGridView1.CurrentCell = dataGridView1.Rows[row].Cells[col + 1];
                    dataGridView1.Focus();
                }
                else if (col == dataGridView1.Columns.Count - 1 && row == dataGridView1.Rows.Count - 1)
                {
                    dataGridView1.Rows.Add(1);
                    dataGridView1.CurrentCell = dataGridView1.Rows[row].Cells[0];
                    dataGridView1.Focus();
                }

【讨论】:

    猜你喜欢
    • 2012-05-04
    • 2013-03-08
    • 2011-06-05
    • 1970-01-01
    • 2014-10-09
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多