Update DataGrid with Row Change

Topics: Actions & Coroutines, Getting Started
Jun 14, 2012 at 6:24 PM
Edited Jun 14, 2012 at 6:28 PM

In extension to this:


	<Button Content="Add Item" Name="AddItem" />
	<Button Content=" Edit Item" Name="EditItem" />
	<DataGrid x:Name="Items"
			<DataGridTextColumn Binding="{Binding ListItem1}" Header="List Item 1" IsReadOnly="True" />
			<DataGridTextColumn Binding="{Binding ListItem2}" Header="List Item 2" IsReadOnly="True" />
private ObservableCollection<ItemClass> _items = new ObservableCollection<ItemClass>();
public ObservableCollection<ItemClass> Items
	get { return _items; }
	set { _items = value; }

private AirSealNotes _selectedItem;
public AirSealNotes SelectedItem
	get { return _selectedItem; }
		_selectedItem = value;
		NotifyOfPropertyChange(() => SelectedItem);

public void AddItem()
	/* Modal Window */
	_windowManager.ShowDialog(new EditItemViewModel(_events));

public void EditItem()
	/* Modal Window */
	_windowManager.ShowDialog(new EditItemViewModel(_events, SelectedItem));

public void Handle(SaveItemMessage message)
		if (message.EditViewModel.NewJob)
			Items.Add(new ItemClass{
				//new items added
			SelectedItem.ListItem1 = message.EditViewModel.ListItem1;
			SelectedItem.ListItem2 = message.EditViewModel.ListItem2;
	catch { }

It is updating the edit to the SelectedItem and even in the collection. However, it is not displaying the edit in the datagrid. What am I missing?

When I Items.Add(/*some data*/); or Items.Remove(SelectedItem); it will automatically update the datagrid with the changes. For some reason unbenounced to it is updating the data, but not the display.

Jun 18, 2012 at 3:00 PM
Edited Jun 18, 2012 at 3:02 PM

One solution to this problem is to make a temp row and fill it with the selected rows data. Then remove the selected row from the collection and add the temp row to the collection. This will fire the event to notify the grid of the row change. It will aslo however place it at the bottom of the grid, if that matters.

Here is what it looks like:

ItemClass tempItem = SelectedItem;

If anyone has a better way of doing this, please show and thanks!