Leandro
Leandro

Reputation: 948

Get previous value of a cell in datagrid

first of all sorry about my english. Im new with C#, wpf and this kind of things. Im using entity framework and I'm binding a datagrid with an ObservableCollection. I need to get the old value of a cell and compare it with the new value. In database the entity has the old value because I haven't save the changes. My problem is that when I try to do a find (context.Find(reference.Id);), the element that returns the find has the new values of the grid.

My XAML has:

<DataGrid x:Name="datagrid" PreviewKeyDown="dataGrid_PreviewKeyDown" AutoGenerateColumns ="False" ItemsSource="{Binding Path=References, ElementName=referenceWindow, Mode=TwoWay}"

My .cs something like this:

 ObservableCollection<Reference> References = new  ObservableCollection<Reference> References(context.References);

How can I get the old value?

Thanks

Upvotes: 0

Views: 3669

Answers (2)

Nawed Nabi Zada
Nawed Nabi Zada

Reputation: 2875

As Kik I would say create a method which is getting fired when the property or properties are changed. It's is a little difficult to give an example when we don't see what you are doing except the databinding. Personally I would skip the twoway mode, and create a method for the whole object.

You can use the RowEditEnding like this:

<DataGrid x:Name="DataGrid" RowEditEnding="DataGrid_OnRowEditEnding"  AutoGenerateColumns="False" Margin="5">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Name, UpdateSourceTrigger=PropertyChanged}"/>
            <DataGridTextColumn Header="Age" Binding="{Binding Age, UpdateSourceTrigger=PropertyChanged}"/>
        </DataGrid.Columns>

    </DataGrid>

And then in the code like this :

private void DataGrid_OnRowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
    {
        var datag = (DataGrid) sender;
        var p = (Person) datag.SelectedValue;
        var p1 = (Person) e.Row.Item;
        Debug.WriteLine(p.Name + ", " + p.Age);
        Debug.WriteLine(p1.Name + ", "+ p1.Age);
    }

This way you will have your database items and the new item. And you can compare and do what you want.

Upvotes: 0

Kik
Kik

Reputation: 428

How about making an event that gets fired in the setter of your folder property, and passing the old name to it. Or you could just have a method that gets called in your setter that does whatever you need to happen. I guess my point is that I believe it will be easier to handle without being concerned with the UI.

Upvotes: 1

Related Questions