You can add a KeyDown event in EventSetter for DataGridRow, like below code shown:
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<EventSetter Event="KeyDown" Handler="personDataGrid_KeyDown"></EventSetter>
</Style>
</DataGrid.RowStyle>
And the code for updating is:
private void personDataGrid_KeyDown(object sender, KeyEventArgs e)
{
try
{
if (e.Key == Key.Enter || Key.Tab == e.Key)
{
e.Handled = true;
Person newP = (sender as DataGridRow).Item as Person;
//Delete the Old item from DB
Person old = context.People.Where(t => t.ID.Equals(newP.ID)).FirstOrDefault();
context.People.Remove(old);
context.SaveChanges();
//Check for duplication and Add the new Items
foreach (Person p in personDataGrid.Items)
{
if (p.ID != newP.ID) { context.People.Add(newP); context.SaveChanges();}
}
}
}
catch (Exception)
{
throw;
}
}
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.