C# DataGridView 数据源未更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18478578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataGridView DataSource Not Updating
提问by TheJediCowboy
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.
我正在使用 Winforms DevExpress,我正在将一个 DataTable 绑定到一个工作正常的 DataGridView。我遇到的问题是我有一些函数可以构建一个新的 DataTable 对象,该对象与原始对象分开,需要替换绑定到 DataGridView 的原始 DataTable。
DataTable originalTable = new DataTable("OriginalTable");
//Populate originalTable
myDataGridControl.DataSource = originalTable;
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,需要将其设置为 myDataGridControl 的数据源。
DataTable newTable = new DataTable("NewTable");
//Populate newTable
//Set newTable as the DataSource for myDataGridControl
myDataGridControl.DataSource = newTable;
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?
我尝试了几种不同的尝试来完成这项工作,例如调用 RefreshDataSource()、Refresh(),将 DataSource 设置为 null。我还没有让它工作。我该怎么做呢?
回答by Khan
Having you tried the following combination?:
您是否尝试过以下组合?:
myDataGridControl.DataSource = originalTable;
myDataGridControl.DataSource = null;
myDataGridControl.DataSource = newTable;
In my experience, setting the DataSource
to null
then to the second source does the trick.
根据我的经验,将DataSource
to null
then 设置为第二个源可以解决问题。
回答by Karl Anderson
Try using a BindingSource
, like this:
尝试使用 a BindingSource
,如下所示:
DataTable sourceTable = new DataTable("OriginalTable");
BindingSource source = new BindingSource();
source.DataSource = sourceTable;
myDataGridControl.Datasource = source;
Now when you want to re-bind, update the sourceTable
variable, like this:
现在当你想重新绑定时,更新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);
Note: Read BindingSource.ResetBindings Methodfor more information about ResetBindings()
.
注意:阅读BindingSource.ResetBindings 方法以获取有关ResetBindings()
.
回答by TheJediCowboy
In case anybody is having trouble even after trying the other suggestions, the following call to PopulateColumns() on the GridControl.MainView property solved the problem for me.
万一有人在尝试其他建议后仍然遇到问题,以下对 GridControl.MainView 属性的 PopulateColumns() 调用为我解决了这个问题。
For example:
例如:
myDataGridControl.MainView.PopulateColumns();
This can also be referenced from the following article with DevExpress. http://www.devexpress.com/Support/Center/Question/Details/Q362978
这也可以从以下 DevExpress 文章中引用。http://www.devexpress.com/Support/Center/Question/Details/Q362978
回答by Dima Sherba
Kinda old topic, but since it bugged me, I decided to share my experience... Binding the source didn't work for me and Datagridview doesn't have "MainView" variable.
有点老话题,但由于它困扰了我,我决定分享我的经验......绑定源对我不起作用,Datagridview 没有“MainView”变量。
I suspect the issue happens, in my case, after running the sorting command:
我怀疑问题发生在我的情况下,在运行排序命令后:
MyDataTable.DefaultView.Sort = "Column Asc";
MyDataTable = MyDataTable.DefaultView.ToTable();
My solution was to rebind again after actions performed:
我的解决方案是在执行操作后再次重新绑定:
myDataGrid.DataSource = MyDataTable;