I have DataGrid and added event as below, performing some calculation for validation, if validation is success then nothing to do. But if validation get failed then highlighted row should be still last selected row, not newly clicked row:-
public void dataGridTable_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (dataGrid.SelectedIndex == lastSelectedIndex)
{
return;
}
/*
Validate method will return true if row validation is fine else false.
*/
if(!validate())
{
// Revert to last selected index.
dataGrid.SelectedIndex = lastSelectedIndex;
}
}
dataGrid.SelectedIndex = lastSelectedIndex;
this code will make selected index as a last selected index (at c# code i check it in debug mode) but in WPF DataGrid still highlight newly clicked row.
Please let me know if i am not clear enough.
Thanks
You can clear the selected cells using the DataGrid.SelectedCells property.
Just do
to clear the current selection.
Or if you want to select cells in the current row of the SelectedIndex you can do
Where
this
is the instance of the DataGrid.