Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF DataGrid cell value changed event

Tags:

c#

wpf

datagrid

I have a setup that looks like this:

// myDG is a DataGrid whose columns are DataGridTextColumn
ObservableCollection<MyItem> myOC;
// myOC is populated with some new MyItem
myDG.ItemsSource = myOC;

where MyItem implements INotifyPropertyChanged. What's the way to properly catch when the user inputs a value into a cell?

I've tried catching PropertyChanged on the MyItems, but I also update the values periodically in the background (the idea is that when the user manually edits the value, a flag is triggered that tells the periodic calculation to avoid overwriting the manually entered data). So PropertyChanged catches everything, including the periodic updates, which I don't want. I suppose it's possible to make this work (by setting a flag when I do the periodic calculation, then checking for absence of flag on the PropertyChanged event handler -- but I want to know if there's a simpler solution.)

I've tried catching myDG.CurrentCellChanged but that's triggered every time the user changes the cell selection, not specifically when they edit cell contents.

Edit: Here is the XAML:

<DataGrid x:Name="myDG" ItemsSource="{Binding}" AutoGenerateColumns="False" Margin="10,10,182,0" VerticalAlignment="Top" Height="329" ClipboardCopyMode="IncludeHeader">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Col1" Binding="{Binding Prop1}" IsReadOnly="True"/>
        <DataGridTextColumn Header="Col2" Binding="{Binding Prop2}" IsReadOnly="False"/>
    </DataGrid.Columns>
</DataGrid>

Here is the MyItem implementation (uses Fody/PropertyChanged):

[ImplementPropertyChanged]
class MyItem : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public string Prop1 { get; set; }
    public string Prop2 { get; set; }

    public MyItem()
    {
        Prop1 = Prop2 = "";
    }
}
like image 500
jcai Avatar asked Dec 22 '15 19:12

jcai


2 Answers

The solution was to catch the CellEditEnding event.

// In initialization
myDG.CellEditEnding += myDG_CellEditEnding;

void myDG_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    if (e.EditAction == DataGridEditAction.Commit)
    {
        var column = e.Column as DataGridBoundColumn;
        if (column != null)
        {
            var bindingPath = (column.Binding as Binding).Path.Path;
            if (bindingPath == "Col2")
            {
                int rowIndex = e.Row.GetIndex();
                var el = e.EditingElement as TextBox;
                // rowIndex has the row index
                // bindingPath has the column's binding
                // el.Text has the new, user-entered value
            }
        }
    }
}
like image 125
jcai Avatar answered Nov 06 '22 08:11

jcai


You can achieve this by Using CellEditEnding event and another thing is in DataGridTextColumn have to add some attributes like below :-

<DataGrid x:Name="myDG" CellEditEnding="myDG_CellEditEnding"  ItemsSource="{Binding}" AutoGenerateColumns="False" Margin="10,10,182,0" VerticalAlignment="Top" Height="329" ClipboardCopyMode="IncludeHeader">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Col1" Binding="{Binding Prop1}" 
 IsReadOnly="True"/>
 <DataGridTextColumn x:Name="dataGridTextColumn"Header="Col2" Binding="{Binding Prop2,  UpdateSourceTrigger=LostFocus, Mode=TwoWay}" Width="*" />
    </DataGrid.Columns>
</DataGrid>

IN C#

 private void myDG_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) {
            string prop1 = (e.Row.Item as DataRowView).Row[1].ToString();
        }
like image 1
Jay Kashyap Avatar answered Nov 06 '22 08:11

Jay Kashyap