【问题标题】:dataGridView BindingContextChanged event not is sync with dataGridView DataSource change, WinformdataGridView BindingContextChanged 事件不与 dataGridView 数据源更改同步,Winform
【发布时间】:2021-05-24 12:46:47
【问题描述】:

我遇到了 dataGridView BindingContextChanged 事件触发的问题。我在表单中有 2 个 datagridview,并为两个网格实现了 BindingContextChanged 事件。

在 BindGrid 方法上,grid1 首先与数据源绑定,但 grid2 的 BindingContextChanged 事件先触发,然后是 grid1,然后在 grid2 与其他数据源绑定后,grid2 的 BindingContextChanged 事件不会触发。 我不知道为什么会发生这种情况,我需要每个 BindingContextChanged 事件应该在各自的网格数据源分配之后触发。请帮我解决这个问题。

下面是示例代码

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.dataGridView1.BindingContextChanged += new System.EventHandler(this.dataGridView1_BindingContextChanged);
        this.dataGridView2.BindingContextChanged += new System.EventHandler(this.dataGridView2_BindingContextChanged);
    }

    private void btnBind_Click(object sender, EventArgs e)
    {
        BindGrid();
    }

    protected void BindGrid()
    {
        DataTable dt1 = new DataTable();
        dt1.Columns.Add("ID", typeof(int));
        dt1.Rows.Add(1);
        dt1.Rows.Add(2);
        dt1.Rows.Add(3);
        BindingSource dataSource = new BindingSource(dt1, null);
        dataGridView1.DataSource = dataSource;

        DataTable dt = new DataTable();
        dt.Columns.Add("ID", typeof(int));
        dt.Rows.Add(1);
        dt.Rows.Add(2);
        dt.Rows.Add(3);

        BindingSource dataSource1 = new BindingSource(dt, null);
        dataGridView2.DataSource = dataSource1;
       
    }

    private void dataGridView1_BindingContextChanged(object sender, EventArgs e)
    {
        // Continue only if the data source has been set.
        if (dataGridView1.DataSource == null)
        {
            return;
        }
    }

    private void dataGridView2_BindingContextChanged(object sender, EventArgs e)
    {
        // Continue only if the data source has been set.
        if (dataGridView2.DataSource == null)
        {
            return;
        }

    }

   
}

【问题讨论】:

  • BindingContextChanged 会被多次引发,无论是在您第一次创建 DataGridView(创建其句柄时)还是当 Form 中的任何 Control 设置 DataSource 时。事件的顺序与控件添加到容器的顺序有关。签入表单的.Designer.cs 文件:this.Controls.Add(this.dataGridView1); 可能插入在this.Controls.Add(this.dataGridView2); 之后。所以,只是颠倒它。 -- 你为什么要处理这个事件?如前所述,任何其他控制都可能导致它升高。它有什么用?
  • 感谢@Jimi,我正在尝试实现 DataGridViewAutoFilter,其中我的网格具有自动生成的列,并且表单中有多个网格,所有必需的自动过滤器都已启用,因为 BindingContextChanged 事件将自动过滤器列呈现为我的第二个网格无法渲染,因为它的事件在数据绑定后永远不会被触发。这是我在 Microsoft 站点 microsoft.com/en-us/download/details.aspx?id=23459 上使用的示例代码
  • 首先,为两个控件引发事件,并且如前所述,按照表单设计器中定义的顺序。但是,您可能不需要它。见How to replace the HeaderCells of a DataGridView with custom headers?。这可以在需要时完成。如果不相关的控件(例如 ComboBox)由于某种原因重置了 BindingContext,您还会重置标题吗? - 请注意,您所指的示例,如果它是我认为的那样,是非常非常古老的。将 .Net 版本至少更新到 .Net Framework 4.8。

标签: c# .net winforms datagridview


【解决方案1】:

我能够重现此问题。我从BindingContextChanged 事件处理程序更改为DataSourceChanged,解决了这个问题。例如

this.dataGridView1.BindingContextChanged += new System.EventHandler(this.dataGridView1_BindingContextChanged);
this.dataGridView2.BindingContextChanged += new System.EventHandler(this.dataGridView2_BindingContextChanged);

到...

this.dataGridView1.DataSourceChanged += new System.EventHandler(this.dataGridView1_DataSourceChanged);
this.dataGridView2.DataSourceChanged += new System.EventHandler(this.dataGridView2_DataSourceChanged);

现在dataGridView1.DataSource 在事件触发时不为空,并且每个都根据修改后的绑定源触发。

【讨论】:

    猜你喜欢
    • 2012-11-29
    • 2017-05-27
    • 1970-01-01
    • 2012-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    • 2011-06-14
    相关资源
    最近更新 更多