C# 如何在 WPF 中刷新数据网格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11324688/
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
How to refresh datagrid in WPF
提问by Johniek Comp
My source is in a MySQL database, I've made an update command and now I need to refresh my DataGrid.
我的源在 MySQL 数据库中,我已经做了一个更新命令,现在我需要刷新我的DataGrid.
MySqlCommand cmd = new MySqlCommand(
"update request set status = " + StatusRequest(value) +
" where id = " + rowView[0].ToString() + "", conn);
MySqlDataReader myReader = cmd.ExecuteReader();
How do I refresh my DataGrid?
我如何刷新我的DataGrid?
采纳答案by JohnnBlade
Reload the datasource of your grid after the update
更新后重新加载网格的数据源
myGrid.ItemsSource = null;
myGrid.ItemsSource = myDataSource;
回答by Nikhil Agrawal
How about
怎么样
mydatagrid.UpdateLayout();
回答by abramlimpin
Try mydatagrid.Items.Refresh()
尝试 mydatagrid.Items.Refresh()
回答by D.Rosado
Bind you Datagrid to an ObservableCollection, and update your collection instead.
将您的 Datagrid 绑定到ObservableCollection,并改为更新您的集合。
回答by KyloRen
I had a lot of trouble with this and this is what helped me get the DataGrid reloaded with the new values. Make sure you use the data type that your are getting the data from to get the latest data values.
我在这方面遇到了很多麻烦,这就是帮助我使用新值重新加载 DataGrid 的原因。确保使用从中获取数据的数据类型来获取最新数据值。
I represented that with SomeDataTypebelow.
我用SomeDataType下面的表示。
DataContext.Refresh(RefreshMode.OverwriteCurrentValues, DataContext.SomeDataType);
Hope this helps someone with the same issues I had.
希望这可以帮助遇到与我相同问题的人。

