如何从datagriview按键事件执行datagridview cellclick事件?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您想要的是知道按下该键的单元格的行和列索引。然后您就可以查找该单元格的值。
为此,只需使用
DataGridView
的CurrentCell
属性。试图人为地创建一个
CellClick
只是自找麻烦。需要注意的一件事是,您可能需要处理
EditingControlShowing
事件,并将KeyPress
处理程序附加到底层编辑控件,因为在DataGridView
单元格不会引发网格级KeyPress
事件。如果您确实想要创建
CellClick
事件,则需要对DataGridView
控件进行子类化,并创建您自己的RaiseCellClick()< /code> 方法,然后调用受保护的
OnCellClick()
方法:但即使这也没有特别帮助您,因为
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 theDataGridView
.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 aKeyPress
handler to the underlying editing control since typing into aDataGridView
cell does not raise the grid levelKeyPress
event.If you really want to create a
CellClick
event you will need to subclass theDataGridView
control, and create your ownRaiseCellClick()
method which then calls the protectedOnCellClick()
method:But even this doesn't particularly help you since the
DataGridViewCellEventArgs
needs to take the row and column indexes in its constructor.