关于datagridview控件的事件
我开发了一个datagridview过滤应用程序。 我使用datagridview的dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
事件进行过滤。 但我想在datagridview单元的按键事件上处理它。 但我没有得到那种类型的活动。
应该在每个按键上发生datagridview事件..
那么任何人都可以告诉我应该将哪个事件用于datagridview?
请帮帮我… thanx
当用户键入特定单元格时, 不会引发DataGridView.KeyPress
事件。 如果您希望在编辑单元格中的内容时每次按键时收到通知,您有两种选择:
-
处理由编辑控件本身直接引发的
KeyPress
事件(您可以使用EditingControlShowing
事件访问该事件)。例如,您可以使用以下代码:
public class Form1 : Form { public Form1() { // Add a handler for the EditingControlShowing event myDGV.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(myDGV_EditingControlShowing); } private void myDGV_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { // Ensure that the editing control is a TextBox TextBox txt = e.Control as TextBox; if (txt != null) { // Remove an existing event handler, if present, to avoid adding // multiple handler when the editing control is reused txt.KeyPress -= new KeyPressEventHandler(txt_KeyPress); // Add a handler for the TextBox's KeyPress event txt.KeyPress += new KeyPressEventHandler(txt_KeyPress); } } private void txt_KeyPress(object sender, KeyPressEventArgs e) { // Write your validation code here // ... MessageBox.Show(e.KeyChar.ToString()); } }
-
创建一个inheritance自标准
DataGridView
控件的自定义类,并覆盖其ProcessDialogKey
方法 。 此方法旨在处理每个键事件,甚至是那些
在编辑控件上发生。 您可以处理该重写方法中的按键,也可以引发您自己的事件,您可以附加一个单独的处理程序方法。