How to refresh datagrid in WPF

[亡魂溺海] 提交于 2019-11-27 10:33:41

问题


My source is in a MySQL database, I've made an update command and now I need to refresh my 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?


回答1:


Reload the datasource of your grid after the update

myGrid.ItemsSource = null;
myGrid.ItemsSource = myDataSource;



回答2:


Try mydatagrid.Items.Refresh()




回答3:


From MSDN -

CollectionViewSource.GetDefaultView(myGrid.ItemsSource).Refresh();



回答4:


Bind you Datagrid to an ObservableCollection, and update your collection instead.




回答5:


How about

mydatagrid.UpdateLayout();



回答6:


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.

I represented that with SomeDataType below.

DataContext.Refresh(RefreshMode.OverwriteCurrentValues, DataContext.SomeDataType);

Hope this helps someone with the same issues I had.



来源:https://stackoverflow.com/questions/11324688/how-to-refresh-datagrid-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!