无法使用bindingsource刷新datagridview

无法使用bindingsource刷新datagridview

本文介绍了无法使用bindingsource刷新datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:

点击添加或删除按钮后,datagridview应使用文档中的最新数据进行刷新。

Goal:
Once clicking on add or delete button, the datagridview should be refreshed with the latest data from document.

问题: / p>

Problem:

我使用的是与datagridview数据源相关的绑定源。

I'm using binding source that is linked with datagridview's datasource.

不同的解决方案,并从不同的论坛读建议,但仍然无法解决这个问题。

I tried everything with different solution and read advise from different forum but still I can't solve this problem.

我也尝试使用这些语法BindingSource.ResetBindings(false), BindingSource.Refresh()等,但没有结果。

I also tried using these syntax "BindingSource.ResetBindings(false)", "BindingSource.Refresh()" etc but no result.

以下链接:

    bSrcStock.DataSource = myProductrepository.GetAllProductList();


    dgridStock.DataSource = null;
    dgridStock.DataSource = bSrcStock;
    bSrcStock.ResetBindings(true);


    dgridStock.Columns[0].Width = 101;
    dgridStock.Columns[1].Width = 65;
    dgridStock.Columns[2].Width = 80;
    dgridStock.Columns[3].Width = 120;
    dgridStock.Columns[4].Width = 90;


推荐答案

我遇到了同样的问题,发现问题是在一个静态构造函数中的BindingSource的初始化(该类是一个单例)。在意识到这一点后,我将代码移动到了调用事件中,最后工作不需要分配null或调用clear方法。希望这有帮助。

I have faced this same issue and found out that the problem is with the initialization of the BindingSource inside a static constructor (The class was a singleton). Upon realizing this, I moved the code to the calling event and it finally worked without needing to assign null or call the clear method. Hope this helps.

这篇关于无法使用bindingsource刷新datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 08:05