Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 关于datagridview控件的事件_C#_.net_Winforms_Datagridview - Fatal编程技术网

C# 关于datagridview控件的事件

C# 关于datagridview控件的事件,c#,.net,winforms,datagridview,C#,.net,Winforms,Datagridview,我为datagridview过滤开发了一个应用程序。我使用了datagridview的dataGridView1\u CellValueChangedobject发送方DataGridViewCellEventArgs e 用于筛选的事件。 但是我想在datagridview单元格的按键事件中处理它。但我不会参加那种类型的活动 应发生datagridview事件 在每个按键上 有人能告诉我datagridview应该使用哪个事件吗 请帮帮我。。。 thanx当用户在特定单元格中键入时,不会引发D

我为datagridview过滤开发了一个应用程序。我使用了datagridview的dataGridView1\u CellValueChangedobject发送方DataGridViewCellEventArgs e 用于筛选的事件。 但是我想在datagridview单元格的按键事件中处理它。但我不会参加那种类型的活动

应发生datagridview事件 在每个按键上

有人能告诉我datagridview应该使用哪个事件吗

请帮帮我。。。 thanx

当用户在特定单元格中键入时,不会引发DataGridView.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());

    }
}
创建从标准DataGridView控件继承的自定义类并重写其。此方法设计用于处理每个关键事件,即使是 发生在编辑控件上的。您可以在被重写的方法内部处理按键,也可以引发您自己的事件,您可以将单独的处理程序方法附加到该事件

当用户在特定单元格中键入时,不会引发DataGridView.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());

    }
}
创建从标准DataGridView控件继承的自定义类并重写其。此方法设计用于处理每个关键事件,即使是 发生在编辑控件上的。您可以在被重写的方法内部处理按键,也可以引发您自己的事件,您可以将单独的处理程序方法附加到该事件


但我也需要行索引和列索引。那么如何在上面的代码中获得rowindex和column索引?@priyanka:最简单的方法是使用。它将返回当前选定的单元格,这将依次公开和属性。如何将e.keychar值设置为datagridview当前单元格?因为当我在应用程序中使用上述事件时,它会将当前单元格显示为空值&因此我无法执行“我的其他操作”。@priyanka:当您获取该单元格时。您只需要在自定义按键事件中设置e.Handled=false,但我也需要行索引和列索引。那么如何在上面的代码中获得rowindex和column索引?@priyanka:最简单的方法是使用。它将返回当前选定的单元格,这将依次公开和属性。如何将e.keychar值设置为datagridview当前单元格?因为当我在应用程序中使用上述事件时,它会将当前单元格显示为空值&因此我无法执行“我的其他操作”。@priyanka:当您获取该单元格时。您只需要在自定义按键事件中设置e.Handled=false。