1

Good morning,

I wanted to know the best way to refresh a grid after making a change to one of the elements in an edit window.

I have a grid with lets say employees. I pass the employee as reference to the edit window...

EmployeeEdit wEdit = new EmployeeEdit(ref selectedEmployee);
wEdit.Show();

If I delete this employee and come back to the main grid, that employee is still there because the grid has not refreshed (I dont want to do a showdialog).

I was thinking about passing a variable as reference and changing that variable upon deleting an employee. Then, when the original grid got focus, check the variable and refresh the grid if I have to.

This seems like a rigged fix and I wanted to know the most efficient way to do this.

Thank you

Walking
  • 467
  • 1
  • 7
  • 23
  • Show the part where you add employees to the grid initially. Actually it seems irrelevant, because it seems you know how to refresh it. So the question is how to know when edit window is closed? Provide an event in that window, rise it when changes are made, subscribe to it in caller window before displaying and refresh grid in handler. – Sinatr May 20 '16 at 12:40
  • When you use an ObservableCollection as data source for your grid and remove the employer from this list when deleting, this will be ready out of the box – unkreativ May 20 '16 at 12:52
  • You can also use `CollectionViewSource` with any `IEnumerable` collection. This will act as an `ObservalbleCollection` without any need to change the Collection's type. – XAMlMAX May 20 '16 at 13:20
  • @XAMIMAX Thank you for all your responses. We are using a csla business framework and I'm unable to figure out how to apply what you guys are suggesting. Please elaborate.. I'm currently assigned the editable root list collection to the item source of the grid.. is it a different item source when you guys refer to collectionviewsource? Thank you – Walking May 20 '16 at 13:31
  • Upon return, if you have a hook / access to the visual grid control showing the items, you should be able to... yourVisualDataGrid.Items.Refresh(); This will force a refresh of the grid to your updated values in the list. – DRapp May 20 '16 at 13:59

1 Answers1

1

You need to subscribe to an event in wEdit object, for example it may have a closing event or a quit event (unfortunately I can't tell from your code).

To subscribe to the event you can write something like this;

 EmployeeEdit wEdit = new EmployeeEdit(ref selectedEmployee);
 wEdit.Closing += (s, e) => 
 {
      //place code to refresh your grid here
 }
 wEdit.Show();

When the event fires it should refresh your grid.

mark_h
  • 5,233
  • 4
  • 36
  • 52
  • I'm going to test this out! what is the `+= (s, e) =>` part? update: just tried it and even though I'm using `this.Close();` in my edit window.. the code never goes into that closing event. – Walking May 20 '16 at 14:48
  • It's a sort of short hand for writing a method, s represents wEdit, the "sender" while e represents the arguments for the event. Look here http://stackoverflow.com/questions/2465040/using-lambda-expressions-for-event-handlers – mark_h May 20 '16 at 14:54