C# 如何让事件向事件处理程序发送参数?

C# 如何让事件向事件处理程序发送参数?,c#,.net,events,datagridview,C#,.net,Events,Datagridview,如何获取事件以向函数发送参数?所以我可以让dataGridView1和dataGridView2都使用函数dataGridView1\u MouseMove private void dataGridView1_MouseMove(object sender, MouseEventArgs e) { if ((e.Button & MouseButtons.Left) == MouseButtons.Left) { // If the mouse move

如何获取事件以向函数发送参数?所以我可以让dataGridView1和dataGridView2都使用函数dataGridView1\u MouseMove

private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
{
    if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
    {
        // If the mouse moves outside the rectangle, start the drag.
        if (dragBoxFromMouseDown != Rectangle.Empty &&
        !dragBoxFromMouseDown.Contains(e.X, e.Y))
        {
            // Proceed with the drag and drop, passing in the list item.                   
            DragDropEffects dropEffect = dataGridView1.DoDragDrop(
                  dataGridView1.Rows[rowIndexFromMouseDown],
                  DragDropEffects.Move);
        }
    }
}
我目前正在使用此函数以及其他一些函数来允许拖放dataGridView1中的行,如何对dataGridView2使用相同的函数

如何对dataGridView2使用相同的函数

如果要使用相同的方法,则永远不要直接参考
dataGridView1

相反,将其更改为使用
sender
作为DataGridView,如下所示:

private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
{
    // Use this instead of dataGridView1
    DataGridView dgv = sender as DataGridView;

    if (dgv == null) // Add some checking
    {
        return;
    }

    if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
    {
         //...
完成此操作后,可以将订阅添加到任意数量的DataGridView实例中,它们都将使用相同的事件处理程序