WPF DataGrid cell value changed event
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();
}
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
}
}
}
}