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?

skeletank
  • 2,880
  • 5
  • 43
  • 75
Johniek Comp
  • 443
  • 1
  • 4
  • 10

7 Answers7

78

Try mydatagrid.Items.Refresh()

abramlimpin
  • 5,027
  • 11
  • 58
  • 97
59

Reload the datasource of your grid after the update

myGrid.ItemsSource = null;
myGrid.ItemsSource = myDataSource;
JohnnBlade
  • 4,261
  • 1
  • 21
  • 22
  • From my testing, this seems to be a lot faster than calling `GetDefaultView` as some of the other answers recommend. – edtheprogrammerguy Jun 30 '20 at 16:38
  • Beware that this re-triggers all kind of events in your DataGrid (for example CheckBox Clicked, ComboBox Selected etc.), this had cost be a good amount of time to figure it out – Zer0 Sep 06 '21 at 08:48
  • This method will work but it is problematic with bindings done in the XAML side of things. – N.I.R.E.X Feb 14 '22 at 10:46
19

From MSDN -

CollectionViewSource.GetDefaultView(myGrid.ItemsSource).Refresh();
user46011
  • 199
  • 1
  • 4
12

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

D.Rosado
  • 5,634
  • 3
  • 36
  • 56
  • what about items being updated? does it handle updates? – Leonardo Feb 26 '16 at 17:09
  • @Leonardo For that you would need your objects to implement the INotifyPropertyChanged interface. https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged%28v=vs.110%29.aspx – D.Rosado Feb 26 '16 at 18:25
  • This doesn't do anything at all. – Kim Homann Nov 28 '19 at 12:20
  • @KimHomann , if you are setting the items through {binding} then clear the class that hold the items , then > your_class.add(new items) again , "a manual refresh" , if you are setting the items through code behind something like (yourDataGrid.ItemsSource = someClass) then use : yourDataGrid.ItemsSource = null; then set the itemsSource again . – The Doctor Jun 21 '22 at 02:25
  • Yet again I use ObservableCollection and it isn't an Observable Collection. I make a change and the observer, i.e. the grid, does nothing. Now I have to dig round the code again to find out what actually makes this work in other places. – Paul McCarthy Jul 31 '23 at 16:28
5

How about

mydatagrid.UpdateLayout();
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
1

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.

KyloRen
  • 2,691
  • 5
  • 29
  • 59
1

If you want update single row:

Create new ObservableCollection and initialize it

public ObservableCollection<myModel> myObservableCollection { get; set; } = new ObservableCollection<myModel>();

Update row

//Get current row as your model
var row = myDataGrid.CurrentItem as myModel;
//Find in your collection index of selected row
var b = myObservableCollection.IndexOf(myObservableCollection.FirstOrDefault(e=>e.Id == row.Id));
//Create a copy of this row
var k = row.Clone() as myModel;
//Here you can mofidy what you want
k.Name = "New name";
k.SomethingId = 2137;
//Replace object in you collection
myObservableCollection[b] = k;

Here's code to clone your model

public object Clone()
{
    return this.MemberwiseClone();
}