【问题标题】:Datagridview tab orderDatagridview 选项卡顺序
【发布时间】:2021-06-06 03:45:54
【问题描述】:

我想在 datagridview 中设置 Tab 键顺序。

product name | unit price  | qty | amount 

我希望在我选择产品的那一刻,应该选择数量单元格。一旦我输入数量并点击标签,它应该会转到下一行..

这是我迄今为止尝试过的

private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Tab)
    {
        dataGridView1.CurrentCell = GetNextCell(dataGridView1.CurrentCell);
        e.Handled = true;
    }
}

private DataGridViewCell GetNextCell(DataGridViewCell currentCell)
{
    int i = 0;
    DataGridViewCell nextCell = currentCell;

    do
    {
        int nextCellIndex = (nextCell.ColumnIndex + 1) % dataGridView1.ColumnCount;
        int nextRowIndex = nextCellIndex == 0 ? (nextCell.RowIndex + 1) % dataGridView1.RowCount 
        : nextCell.RowIndex;
        nextCell = dataGridView1.Rows[nextRowIndex].Cells[nextCellIndex];
        i++;
    } while (i < dataGridView1.RowCount * dataGridView1.ColumnCount && nextCell.ReadOnly);

    return nextCell;
}

【问题讨论】:

    标签: c# .net winforms datagridview desktop-application


    【解决方案1】:

    很难理解您的要求是什么。所以,我猜这可能就是你要找的。​​p>

    如果目标是只允许用户在“数量”单元格中输入值,并强制网格选择始终是“数量”列中的一个单元格,那么看起来你可能会让这更复杂它必须是。

    你评论了……

    “我希望在我选择产品的那一刻,应该选择数量单元格。”

    您没有具体说明您是指该行中的任何其他单元格还是仅指“产品”单元格。我会假设你的意思是任何细胞。如果是这种情况,那么我建议您连接网格CellMouseUp 事件并将“数量”单元格设置为选定单元格。像……

    private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e) {
      if (e.RowIndex >= 0)
        dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells["Quantity"];
    }
    

    关于在网格中四处移动,您的Tab 键似乎以一种过于复杂的方式工作,这可以简化。我的问题是如果用户按下右箭头键或左箭头键怎么办?当前代码没有对此进行检查,用户可以将网格选定的单元格移动到另一个不是“数量”列的单元格。我猜你可能也想检查这些键。

    下面的代码也将满足第二个要求……

    “一旦我输入数量并点击标签,它应该会转到下一行..”

    下面是网格KeyUp 事件,其中包含移动Tab 键的简化版本,此外还添加了对左右箭头键的检查。

    private void dataGridView1_KeyUp(object sender, KeyEventArgs e) {
      DataGridViewRow curRow = dataGridView1.CurrentRow;
      if (e.KeyCode == Keys.Tab) {
        if (curRow.Index == dataGridView1.Rows.Count - 1) {
          dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells["Quantity"];
        }
        else {
          dataGridView1.CurrentCell = dataGridView1.Rows[curRow.Index + 1].Cells["Quantity"];
        }
      }
      if (e.KeyCode == Keys.Right || e.KeyCode == Keys.Left) {
        dataGridView1.CurrentCell = dataGridView1.Rows[curRow.Index].Cells["Quantity"];
      }
    }
    

    根据 OP 评论编辑...

    同样,您需要“编辑”您的问题,并明确在用户按下 Tab 键时您想要什么行为。从您的评论中,我只能猜测以下是您要查找的内容。

    private void dataGridView1_KeyUp(object sender, KeyEventArgs e) {
      DataGridViewCell curCell = dataGridView1.CurrentCell;
      DataGridViewRow curRow = dataGridView1.CurrentRow;
      switch (e.KeyCode) {
        case Keys.Tab:
          if (dataGridView1.Columns[curCell.ColumnIndex].Name == "Amount") {
            if (curRow.Index == dataGridView1.Rows.Count - 1) {
              dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells["ProductName"];
            }
            else {
              dataGridView1.CurrentCell = dataGridView1.Rows[curRow.Index + 1].Cells["ProductName"];
            }
          }
          else {
            if (dataGridView1.Columns[curCell.ColumnIndex].Name == "UnitPrice") {
              dataGridView1.CurrentCell = dataGridView1.Rows[curRow.Index].Cells["Quantity"];
            }
          }
          break;
      }
    }
    

    【讨论】:

    • 如果我从网格中选择产品,那么应该选择 qty 单元格,一旦我输入数量,然后我按 Tab,它应该带我到下一行产品名称单元格
    • @user9914033 … 你是什么意思… “如果我从网格中选择产品,那么应该选择 qty 单元格…” … 这很好并且可行,但是你继续... “一旦我输入数量然后我按 Tab 然后它应该带我到下一行产品名称单元格” ....? ...如果代码将选择移至“产品”调用,那么您的第一条评论不适用?这是一个矛盾......你不能同时拥有它。您应该编辑您的问题并清楚地描述按下 Tab 键时您正在寻找的行为。
    • 这里是要求
    • 如果用户选择产品,则应选择数量单元格,这样他就不必进入单价和金额单元格,指定数量后,应选择下一行产品单元格
    • @user9914033... 什么意思... “如果用户选择产品。” ...?用户是否在产品单元格上“单击”以“选择产品”?用户“TAB”是否可以“选择产品”?正如我之前所说的你评论的第一部分......“选择产品”......最后一部分...... “应该选择下一行产品单元格” ...没有有道理,又是矛盾的。你没有明确自己想要什么。
    猜你喜欢
    • 2011-04-02
    • 2012-08-29
    • 2015-11-14
    • 2012-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-20
    相关资源
    最近更新 更多