【问题标题】:How to access DataGridView in an event handler?如何在事件处理程序中访问 DataGridView?
【发布时间】:2016-07-21 16:54:29
【问题描述】:

我有一个TabControl,其中包含一个DataGridView 和一个ToolStripButtonDataGridView 启用了多项选择。 toolStripButton 的功能是从DataGridView 中删除行。

DataGridView 有一个 BindingSource:

partial class MyView 
{
    ...
    this.myBindingSource  = new System.Windows.Forms.BindingSource(this.components);
    ...
    this.dataGridView.DataSource = this.myBindingSource;
    this.dataGridView.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
    this.dataGridView.MultiSelect = true;
    ....
    this.tsbDelete.Click += new System.EventHandler(this.tsbDelete_Click);
    ...

我有一个删除按钮的事件处理程序,如下所示:

public partial class MyView : System.Windows.Forms.UserControl, IMyView
    {
    ...
    private void tsbDelete_Click (object sender, System.EventArgs e) 
    {
        var current = myBindingSource.Current;
        ...
    }

current 是一个单一的选择。如果已选择多行,则为最后选择的行。我不知道如何选择所有行。困难在于DataGridView 在事件处理程序中不可用(或者我不知道如何访问它,这就是问题所在)。如果可以,我可以访问DataGridView.SelectedRowssender 是按钮,它不直接连接到 DataGridView。
我该怎么做?

完全指定:

System.Windows.Forms.TabControl
System.Windows.Forms.DataGridView
System.Windows.Forms.BindingSource
System.Windows.Forms.ToolStripButton

【问题讨论】:

  • 您可能无法访问 DGV,因为您的处理程序是公共的,而您的构造函数类不是?我一直能够毫无问题地从处理程序中访问元素。
  • 嘿,我做过更愚蠢的事情,所以不要难过。我已将我的回复添加为答案。请标记为答案。很高兴我能帮上忙。

标签: c# .net datagridview


【解决方案1】:

您可能无法访问 DGV,因为您的处理程序是公共的,而您的构造函数类不是?我一直能够毫无问题地从处理程序中访问元素。

【讨论】:

    猜你喜欢
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-13
    • 2011-02-09
    相关资源
    最近更新 更多