【问题标题】:Handling Enter key in DataGridView处理 DataGridView 中的 Enter 键
【发布时间】:2016-11-13 20:06:33
【问题描述】:

我有一个有 5 列的 datagridview。 datagridview 中的列是[Item Code, Description, Quantity,Price & Total]。

应该如何工作: 当用户输入 itemcode 时,描述和价格应该从数据库中获取并在相应的字段中显示结果。然后数量应该由用户输入。

我想要什么: 我希望当用户在第一列(项目代码)中输入值时,下一个要选择的单元格应该是第三列(数量)。然后,如果输入数量并且用户按下回车按钮,则选择的下一个单元格应该是下一行的第一列(项目代码)。

问题: 我已经尝试了几个代码,但它不工作。问题是当在第 1 列(Item Code)中输入项目代码时,下一个单元格的位置是第 3 列(数量)的 2nd Row。它应该移动到同一行的第三列(数量)即第一行

以下是我使用过的代码:

private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
    {
        currentRow = dataGridView1.CurrentCell.RowIndex;
        currentColumn = dataGridView1.CurrentCell.ColumnIndex;

    }

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {

        if (currentColumn == dataGridView1.Columns.Count - 5)
        {                
            dataGridView1.CurrentCell = dataGridView1[currentColumn + 2, currentRow];

        }
        else
        {
            dataGridView1.CurrentCell = dataGridView1[currentColumn - 2, currentRow + 1];
        }
    }

【问题讨论】:

  • 使用foreach循环来确定datagridview中的哪一行被选中了..
  • 我应该怎么做? @MethodMan
  • 谷歌搜索如何使用 foreach 循环找到 DataGriadView 的选定行干杯..
  • 这个问题太多了。请将问题缩小到一个问题。例如,在第一个单元格中输入产品代码时加载产品名称和价格。或者另一个问题可以处理DataGridView 中的 Enter 键,如果您搜索,您会找到一些答案。
  • 谢谢@RezaAghaei 我现在已经改变了我的问题。

标签: c# datagridview


【解决方案1】:

感谢this 的帖子。

我进行了一些编辑,这是我的有效代码。

 protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
    {
        icolumn = dataGridView1.CurrentCell.ColumnIndex;
        irow = dataGridView1.CurrentCell.RowIndex;
        int i = irow;
        if (keyData == Keys.Enter)
        {
            if (icolumn == dataGridView1.Columns.Count - 5)
            {
                   dataGridView1.CurrentCell = dataGridView1[icolumn + 2, irow];
            }
            else
            {
                dataGridView1.CurrentCell = dataGridView1[icolumn - 2, irow + 1];
            }
           return true;
        }


        return base.ProcessCmdKey(ref msg, keyData);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    • 2018-04-16
    相关资源
    最近更新 更多