Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does WPF DataGrid fire an event when a row is added / removed?

Tags:

c#

.net

wpf

I wish to recalculate things everytime a DataGrid gets more rows or some are removed. I tried to use the Loaded event, but that was fired only once.

I found AddingNewItem, but that is fired before it has been added. I need to do my stuff afterwards.

There's also LayoutUpdated, which works, but I'm afraid it's not wise to use it, because it fires way too often for my purposes.

like image 721
Tower Avatar asked Jul 02 '12 12:07

Tower


People also ask

Can user add rows DataGrid WPF?

WPF DataGrid (SfDataGrid) provides built-in row called AddNewRow. It allows user to add a new row to underlying collection. You can enable or disable by setting SfDataGrid.

Can user delete rows WPF?

By default, the user can delete rows by selecting one or more rows and pressing the DELETE key.

What is the default event of DataGrid?

By default, the DataGrid control generates columns automatically when you set the ItemsSource property. The type of column that is generated depends on the type of data in the column.

What is the difference between grid and DataGrid in WPF?

A Grid is a control for laying out other controls on the form (or page). A DataGrid is a control for displaying tabular data as read from a database for example.


1 Answers

If your DataGrid is bound to something, I think of two ways of doing this.

You could try getting the DataGrid.ItemsSource collection, and subscribing to its CollectionChanged event. This will only work if you know what type of collection it is in the first place.

// Be warned that the `Loaded` event runs anytime the window loads into view,
// so you will probably want to include an Unloaded event that detaches the
// collection
private void DataGrid_Loaded(object sender, RoutedEventArgs e)
{
    var dg = (DataGrid)sender;
    if (dg == null || dg.ItemsSource == null) return;

    var sourceCollection = dg.ItemsSource as ObservableCollection<ViewModelBase>;
    if (sourceCollection == null) return;

    sourceCollection .CollectionChanged += 
        new NotifyCollectionChangedEventHandler(DataGrid_CollectionChanged);
}

void DataGrid_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    // Execute your logic here
}

The other solution would be to use an Event System such as Microsoft Prism's EventAggregator or MVVM Light's Messenger. This means your ViewModel would broadcast a DataCollectionChanged event message anytime the bound collection changes, and your View would subscribe to receive these messages and execute your code anytime they occur.

Using EventAggregator

// Subscribe
eventAggregator.GetEvent<CollectionChangedMessage>().Subscribe(DoWork);

// Broadcast
eventAggregator.GetEvent<CollectionChangedMessage>().Publish();

Using Messenger

//Subscribe
Messenger.Default.Register<CollectionChangedMessage>(DoWork);

// Broadcast
Messenger.Default.Send<CollectionChangedMessage>()
like image 70
Rachel Avatar answered Oct 03 '22 20:10

Rachel