无法使用bindingsource刷新datagridview

目标:
单击“添加”或“删除”按钮后,应使用文档中的最新数据刷新datagridview。

问题:

通过删除或添加新数据进行更改后,无法刷新datagridview。

我正在使用与datagridview的数据源链接的绑定源。

我用不同的解决方案尝试了一切,并从不同的论坛阅读建议,但我仍然无法解决这个问题。

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

以下链接:

如何刷新绑定源

http://www.eggheadcafe.com/community/aspnet/2/10114324/datagridview-refresh-from-another-form.aspx

http://blogs.msdn.com/b/dchandnani/archive/2005/03/15/396387.aspx

http://bytes.com/topic/c-sharp/answers/812061-problem-refresh-datagridview

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方法。 希望这可以帮助。

无需定义列(除非您真的想……)

然后每次添加或删除列表中的内容时,只需调用refreshDataGridView方法…

  public List ciList = new List(); CustomItem tempItem = new CustomItem(); tempItem.Name = "Test Name"; ciList.add(tempItem); refreshDataGridView(); private void refreshDataGridView() { dataGridView1.DataSource = typeof(List<>); dataGridView1.DataSource = ciList; dataGridView1.AutoResizeColumns(); dataGridView1.Refresh(); }