DataGridView数据源未更新 [英] DataGridView DataSource Not Updating

查看:67
本文介绍了DataGridView数据源未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Winforms DevExpress,并将DataTable绑定到工作正常的DataGridView.我遇到的问题是,我有一些函数将构建一个新的DataTable对象,该对象与原始对象分离,该对象需要替换绑定到DataGridView的原始DataTable.

I am using Winforms DevExpress and I am binding a DataTable to a DataGridView which is working fine. The problem I am having is that I have some functions that will build a new DataTable object which is separate from the original which needs to replace the original DataTable that is bound to the DataGridView.

DataTable originalTable = new DataTable("OriginalTable");

//Populate originalTable 

myDataGridControl.DataSource = originalTable;

上面的代码都可以正常工作,但是以下代码创建了一个新的DataTable,需要将其设置为myDataGridControl的数据源.

Everything works fine with the code above, but the following code creates a new DataTable and needs to be set as the DataSource for myDataGridControl.

DataTable newTable = new DataTable("NewTable");

//Populate newTable

//Set newTable as the DataSource for myDataGridControl
myDataGridControl.DataSource = newTable;

我已经尝试了多种尝试来完成这项工作,例如调用RefreshDataSource(),Refresh(),将DataSource设置为null.我还没有开始工作.我该怎么办?

I have tried several different attempts to make this work such as calling RefreshDataSource(), Refresh(), setting DataSource to null. I have not gotten it to work yet. How do I do this?

推荐答案

尝试使用 BindingSource ,如下所示:

DataTable sourceTable = new DataTable("OriginalTable");
BindingSource source = new BindingSource();
source.DataSource = sourceTable;
myDataGridControl.Datasource = source;

现在,当您想重新绑定时,更新 sourceTable 变量,如下所示:

Now when you want to re-bind, update the sourceTable variable, like this:

sourceTable = new DataTable("NewTable");

// If the structure of `OriginalTable` and `NewTable` are the same, then do this:
source.ResetBindings(false);

// If the structure of `OriginalTable` and `NewTable` are different, then do this:
source.ResetBindinds(true);

注意:有关详细信息,请阅读 BindingSource.ResetBindings方法有关 ResetBindings()的信息.

Note: Read BindingSource.ResetBindings Method for more information about ResetBindings().

这篇关于DataGridView数据源未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆