Take the 2-minute tour ×

I am using a DataGrid to show data to the user.

I am allowing the user to edit the data and add new rows. Is it possbile to use the built-in funcionality of CanUserDeleteRows to allow the user only to delete rows that he just added.

So usecase-description:

1) User opens datagrid 2) datagrid shows its contents 3) user adds a few rows 4) user deletes one (or all) of his added rows. 5) user is not able to delete rows that were in the datagrid when it initally loaded

...so only "new" rows should be able to get deleted. What approach is best here? I am a little bit out of ideas at the moment...

UPDATE: To satisfty your comments ;)

I am using an ObservableCollection of an custom class to fill the datagrid. So the "problem" is that I don't actually know ho to "intercept" the delete-event nor how to do it without breaking the mvvm-pattern.

share|improve this question
What data Type are you using for Bind DataGrid? If DataTable then DataRow has Added state if new row added in DataTable. –  Jignesh Thakker Oct 1 '12 at 15:51

2 Answers 2

You have two choices :

1 `Disconnected Mode`

You save your new rows on cache, You use GetChanges method in order to get added rows.

//GetChanges return added rows before update datasource from caching
var newRows = YourDataTable.GetChanges(DataRowState.Added);

You add another Data Column, you set value of this column every time that you add new rows.

DataColumn columnFlag = new DataColumn("FlagDelete", typeof(bool));

Delete Button is visible by according with column value

2 `Connected Mode` 

You create in your database new column, you realize server calls in order to get your datas, and visualize your function delete according to the value of column flag

share|improve this answer
up vote 0 down vote accepted

As I am using ObservableCollections and so on, I used a working approach, a little bit based on Aghilas Yakoub's answer.

I added a Remove-Button to the DataGrid thats visibility is bound to a IsNew-Property.

          <Button Content="Remove" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabControl}}, Path=DataContext.BottomDetailVM.DeleteCommand}" Visibility="{Binding IsNew, Converter={StaticResource Bool2VisibilityConverter}, FallbackValue=Collapsed}" CommandParameter="{Binding}" />

When the usere clicks the button the command is executed with the current object (that is shown in the row) as a parameter.

public ICommand DeleteRangfolgeCommand
     return new ActionCommand<MyOwnViewModel>(ExecuteDelete);

private void ExecuteDelete(MyOwnViewModel viewModelToDelete)

Btw: I am using the RelayCommand-implementation (in my case called ActionCommand) from the MSDN-Magazine.

share|improve this answer

Your Answer


