How to refresh datagrid in WPF

2019-01-21 23:32发布

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?

6条回答
叼着烟拽天下
2楼-- · 2019-01-21 23:51

From MSDN -

CollectionViewSource.GetDefaultView(myGrid.ItemsSource).Refresh();
查看更多
Evening l夕情丶
3楼-- · 2019-01-21 23:54

Try mydatagrid.Items.Refresh()

查看更多
唯我独甜
4楼-- · 2019-01-21 23:56

Reload the datasource of your grid after the update

myGrid.ItemsSource = null;
myGrid.ItemsSource = myDataSource;
查看更多
贼婆χ
5楼-- · 2019-01-21 23:57

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

查看更多
劫难
6楼-- · 2019-01-22 00:01

How about

mydatagrid.UpdateLayout();
查看更多
Lonely孤独者°
7楼-- · 2019-01-22 00:02

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.

查看更多
登录 后发表回答