Question

Suppose I have the following table:

CREATE TABLE EXAMPLETABLE (
     ID NUMBER(10,0) NOT NULL,
     DELETIONDATE DATE, 
     NAME VARCHAR2(100 CHAR) NOT NULL, 
     UNIQUE (DELETIONDATE, NAME));

How to soft delete an item from a WPF DataGrid? Soft deletion is the operation below:

UPDATE EXAMPLETABLE 
SET DELETIONDATE = NOW()
WHERE ID = SOMEID;

Should I handle the KeyPress event? In the form I have a Save button, should I do some treatment before calling entities.SaveChanges()?

Was it helpful?

Solution

You havn't provided a lot of information about how you use the datagrid so I will have to make some assumptions.

If you want the "soft deletion" to happen when the user deletes a row from the datagrid you need to enable deletion of rows. You do that by setting CanUserDeleteRows to True.

I assume that you are using some kind of databinding to bind the rows in the datagrid. When a row is deleted the underlying object in ItemsSource is removed from that collection. If you use a collection implementing INotifyCollectionChanged (like ObservableCollection) an event is fired when that happens.

You can listen to that event and modify the underlying model object accordingly by setting DeletionDate to DateTime.Now. You will then have to call entities.SaveChanges() to push that change to the database.

private void ReloadData()
{
    var viewSource = (CollectionViewSource)this.FindResource("aTableViewSource");
    var aTableRows = new ObservableCollection<aTable>
    (
        this.entities.aTable
            .Where(fibra => !fibra.DELETIONDATE.HasValue)
            .Take(10)
            .ToList()
    );

    fibras.CollectionChanged += EventHandler;
    fibrasViewSource.Source = aTableRows;
}

void EventHandler(object sender, NotifyCollectionChangedEventArgs e)
{
    if (e.Action == NotifyCollectionChangedAction.Remove)
    {
        foreach(var oldItem in e.OldItems)
        {
            ((aTable)oldItem).DELETIONDATE = DateTime.Now;
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top