如何从datagriview按键事件执行datagridview cellclick事件?

发布于 2025-01-08 05:46:32 字数 502 浏览 1 评论 0原文

     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            }
    }

我想从按键事件执行单元格单击事件。怎么办呢?

     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            }
    }

i want to perform the cell click event from keypress event. how to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天赋异禀 2025-01-15 05:46:33

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

为此,只需使用 DataGridViewCurrentCell 属性。

试图人为地创建一个CellClick只是自找麻烦。

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


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

public void RaiseCellClick(int row, int column)
{
    DataGridViewCellEventArgs e = new DataGridViewCellEventArgs(row, column);
    base.OnCellClick(e);
}

但即使这也没有特别帮助您,因为 DataGridViewCellEventArgs 需要在其中获取行和列索引构造函数。

It sounds like what you want is to know the row and column index of the cell that the key is pressed in. From that you will then be able to look up the value of the cell.

To do this just use the CurrentCell property of the DataGridView.

Trying to artificially create a CellClick is just asking for trouble.

One thing to note is that you will probably need to handle the EditingControlShowing event and attach a KeyPress handler to the underlying editing control since typing into a DataGridView cell does not raise the grid level KeyPress event.


If you really want to create a CellClick event you will need to subclass the DataGridView control, and create your own RaiseCellClick() method which then calls the protected OnCellClick() method:

public void RaiseCellClick(int row, int column)
{
    DataGridViewCellEventArgs e = new DataGridViewCellEventArgs(row, column);
    base.OnCellClick(e);
}

But even this doesn't particularly help you since the DataGridViewCellEventArgs needs to take the row and column indexes in its constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文