WPF DataGrid Move to last selected row without highlight recent clicked row

552 views Asked by At

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

1

There are 1 answers

0
Bilal Bashir On

You can clear the selected cells using the DataGrid.SelectedCells property.

Just do

this.SelectedCells.Clear();

to clear the current selection.

Or if you want to select cells in the current row of the SelectedIndex you can do

this.SelectAllCells();
DataGridCellInfo[] cells = this.SelectedCells.Where(x => this.Items.IndexOf(x.Item) == this.SelectedIndex).ToArray();
this.SelectedCells.Clear();
foreach (DataGridCellInfo cell in cells)
{
     this.SelectedCells.Add(cell);
}

Where this is the instance of the DataGrid.