WPF DataGrid - event for new rows?

I am using WPF version of DataGrid ( .Net 3.5 SP 1 from Toolkit )

What event can I subscribe to determine when a new row is added? (for example, when the user moves the cursor down or presses Enter, a new empty row is added to the grid).

Ultimately, what I want to do is use this event to compute the default values โ€‹โ€‹and put them on a new line.

The grid is bound to a DataTable , if that matters.

+3
source share
4 answers

Instead of working with events in your view (grid), I would recommend looking at the related object and placing my logic there. This preserves your business logic with your business objects.

Since you're attached to a DataTable , the easiest way is to simply subscribe to a DataTable.TableNewRow .

+6
source

The event you are looking for is the DataGrid.AddingNewItem event . This event allows you to customize a new object as you wish, and then apply it to the NewItem property for AddingNewItemEventArgs.

Xaml:

  <DataGrid Name="GrdBallPenetrations" ItemsSource="{Binding BallPenetrationCollection}" SelectedItem="{Binding CurrentSelectedBallPenetration}" AutoGenerateColumns="False" IsReadOnly="False" CanUserAddRows="True" CanUserDeleteRows="True" IsSynchronizedWithCurrentItem="True" AddingNewItem="GrdBallPenetrations_AddingNewItem"> 

Code behind:

 private void GrdBallPenetrations_AddingNewItem(object sender, AddingNewItemEventArgs e) { e.NewItem = new BallPenetration { Id = Guid.NewGuid(), CarriageWay = CariageWayType.Plus, LaneNo = 1, Position = Positions.BetweenWheelTracks }; } 
+7
source

Objects are saved (inserted or updated) when the user leaves the line that he edited. Moving to another cell on the same line updates the corresponding property by means of data binding, but so far does not signal the model (or data access level). The only useful event is DataGrid.RowEditEnding. This fires just before the modified line is executed.

Xaml

 <DataGrid ... RowEditEnding="MyDataGrid_RowEditEnding"> </DataGrid> 

Code for

 private void MyDataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e) { // Only act on Commit if (e.EditAction == DataGridEditAction.Commit) { var newItem = e.Row.DataContext as MyDataboundType; if (newItem is NOT in my bound collection) ... handle insertion... } } 

All credits for this solution go to Diederik Krolls ( Original post ). My respect.

+6
source

I add this because I spent almost 2 hours trying to figure out how to get the DataGrid to add a new row when you are tied to a collection of view models and you need to control the construction of these view models.

So the setup is that you have an ObservableCollection<MyViewModel> associated with the ItemsSource your DataGrid. You need to create MyViewModel yourself at the level of the view model.

This is what a DataGrid looks like when it automatically adds a row:

  • When he creates this empty line at the bottom, he creates a new instance of MyViewModel that binds to the line, invoking the default constructor for the type. Who knows why he does this, but if MyViewModel does not have a default constructor, he simply will not be able to show this empty string. This is probably the place you are stuck for because you do not have a default constructor because you need to create the object yourself. Unfortunately, you will need to add and add. Note again that if the element type is an interface, this is doomed to failure. The type of the collection item must be a concrete class with a standard constructor.
  • Now it waits until the user proceeds to edit the line, after which he begins to add correctly.
  • AddingNewItem arises: here you can intercept the add operation and disable the default constructor created using your own instance. AddingNewItemEventArgs.NewItem has a setter, and you can change the current element to your own.
+2
source

Source: https://habr.com/ru/post/958780/


All Articles