如何从datagridview keypress事件执行datagridview单元格单击事件?

时间:2012-02-22 15:13:30

标签: c# .net datagridview event-handling

     private void item_grid_CellClick(object sender, DataGridViewCellEventArgs e)
    {
         if (e.ColumnIndex==taxone_col_index || e.ColumnIndex==taxtwo_col_index)
        {


        }

    }

    private void item_grid_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.Return)
        {
            e.Handled = true;

           item_grid.CellClick; // i did this but its not working            }
    }

我想从按键事件中执行单元格点击事件。怎么做?

1 个答案:

答案 0 :(得分:2)

听起来你想要的是知道按下键的单元格的行和列索引。然后你可以查找单元格的值。

要执行此操作,只需使用CurrentCell的{​​{1}}属性。

尝试人为地创建DataGridView只是在寻找麻烦。

需要注意的一点是,您可能需要处理CellClick事件并将EditingControlShowing处理程序附加到基础编辑控件,因为键入KeyPress单元格不会引发网格级DataGridView事件。


如果确实想要创建KeyPress事件,则需要对CellClick控件进行子类化,然后创建自己的DataGridView方法,然后调用受保护的RaiseCellClick()方法:

OnCellClick()

但是,即使这对您来说也没有特别的帮助,因为public void RaiseCellClick(int row, int column) { DataGridViewCellEventArgs e = new DataGridViewCellEventArgs(row, column); base.OnCellClick(e); } 需要在其构造函数中获取行和列索引。