【问题标题】:Pass datagridview to event - Combine Events将 datagridview 传递给事件 - 组合事件
【发布时间】:2014-07-18 20:52:06
【问题描述】:

我在一个表单中有两个数据网格视图。我有多个事件对每个 DGV 执行相同的操作。是否可以将 DGV 传递给事件,以便我可以删除其中一个?

private void mainLogDGV_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == mainLogDGV.Columns["Comment"].Index)
    {
        mainLogDGV.ReadOnly = false;
    }
}

private void filteredLogDGV_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == filteredLogDGV.Columns["Comment"].Index)
    {
        filteredLogDGV.ReadOnly = false;
    }
}

【问题讨论】:

  • 当然。查看 sender 参数。将它投射到 DataGridView 上,你就在路上了..
  • :) 我是 C# 新手。谢谢TaW
  • 两个gridview是否有相同的列

标签: c# datagridview


【解决方案1】:

这样试试

private void mainLogDGV_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
   if (((DataGridView)sender).Name == dataGridView1.Name) 
   { '//Grid one Process }
   else if (((DataGridView)sender).Name == dataGridView2.Name) 
   { '//Grid TweProcess  }

}

private void filteredLogDGV_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
      if (((DataGridView)sender).Name == dataGridView1.Name) 
      { '//Grid one Process }
      else if (((DataGridView)sender).Name == dataGridView2.Name) 
      { '//Grid TweProcess  }
}

【讨论】:

    【解决方案2】:

    您可以使用以下方式引用调用 DGV;

    DataGridView SendingDGV = ((DataGridViewCell)sender).DataGridView
    

    然后使用 SendingDGV 作为参考进行更改。

    【讨论】:

      猜你喜欢
      • 2019-12-17
      • 1970-01-01
      • 1970-01-01
      • 2020-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-24
      相关资源
      最近更新 更多