The*_*boy 5 c# data-binding datatable devexpress winforms
我正在使用Winforms DevExpress,我正在将DataTable绑定到DataGridView,它正常工作.我遇到的问题是我有一些函数将构建一个新的DataTable对象,该对象与原始文件分开,需要替换绑定到DataGridView的原始DataTable.
DataTable originalTable = new DataTable("OriginalTable");
//Populate originalTable
myDataGridControl.DataSource = originalTable;
Run Code Online (Sandbox Code Playgroud)
使用上面的代码一切正常,但下面的代码创建一个新的DataTable,需要设置为myDataGridControl的DataSource.
DataTable newTable = new DataTable("NewTable");
//Populate newTable
//Set newTable as the DataSource for myDataGridControl
myDataGridControl.DataSource = newTable;
Run Code Online (Sandbox Code Playgroud)
我尝试了几种不同的尝试来调用RefreshDataSource(),Refresh(),将DataSource设置为null.我还没有让它工作.我该怎么做呢?
尝试使用a BindingSource,像这样:
DataTable sourceTable = new DataTable("OriginalTable");
BindingSource source = new BindingSource();
source.DataSource = sourceTable;
myDataGridControl.Datasource = source;
Run Code Online (Sandbox Code Playgroud)
现在,当您想要重新绑定时,更新sourceTable变量,如下所示:
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);
Run Code Online (Sandbox Code Playgroud)
注意:有关的更多信息,请阅读BindingSource.ResetBindings方法ResetBindings().
你试过以下组合吗?:
myDataGridControl.DataSource = originalTable;
myDataGridControl.DataSource = null;
myDataGridControl.DataSource = newTable;
Run Code Online (Sandbox Code Playgroud)
根据我的经验,将DataSourceto 设置为null第二个源可以解决问题.