12

How would one just clear it?

There is UnselectAll or UnselectAllCells methods, but they don't work. Also, setting SelectedItem = null or SelectedIndex = -1 does not work either.

Also I do not want to completely disable the selection, I just want to clear the current selection (if any) and set a new selection programmatically.

5 Answers 5

25
dataGrid.UnselectAll()

For rows mode

5

To clear current selection, you can use this code (as you see it is different whether the mode is Single or Extended)

if(this.dataGrid1.SelectionUnit != DataGridSelectionUnit.FullRow)
    this.dataGrid1.SelectedCells.Clear();

if (this.dataGrid1.SelectionMode != DataGridSelectionMode.Single) //if the Extended mode
    this.dataGrid1.SelectedItems.Clear();
else 
    this.dataGrid1.SelectedItem = null;

To select new items programmatically, use this code:

if (this.dataGrid1.SelectionMode != DataGridSelectionMode.Single) 
{    //for example, select first and third items
    var firstItem = this.dataGrid1.ItemsSource.OfType<object>().FirstOrDefault();
    var thirdItem = this.dataGrid1.ItemsSource.OfType<object>().Skip(2).FirstOrDefault();

    if(firstItem != null)
        this.dataGrid1.SelectedItems.Add(firstItem);
    if (thirdItem != null)
        this.dataGrid1.SelectedItems.Add(thirdItem);
}
else
    this.dataGrid1.SelectedItem = this.dataGrid1.ItemsSource.OfType<object>().FirstOrDefault(); //the first item
8
  • @miliu Yes, my previous code wasn't correct. I fixed my answer.
    – vortexwolf
    May 17, 2011 at 9:57
  • @vortex: I don't know if I did something wrong, but this new code for clearing the selection doesn't work for me either. I'm using this code in MouseDoubleClick event.
    – newman
    May 18, 2011 at 0:25
  • For my case, I found a workaround: The selection disappears when I refresh the datacontext by calling "Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() => grid.Items.Refresh()));"
    – newman
    May 18, 2011 at 0:35
  • Just a note...WPF DataGrid is great, but a few minor things drive me crazy.
    – newman
    May 18, 2011 at 0:37
  • 1
    @GeorgeBirbilis My answer was long ago and I don't remember. But my code has if-else combination which uses either SelectedItems.Clear or SelectedItem = null depending on the selection mode. If new SelectionMode values appear, I think this if-else should be rewritten.
    – vortexwolf
    Jul 8, 2015 at 22:50
2
DataGrid.UnselectAllCells()

It works for me.

1

Disabling and reenabling the DataGrid worked for me.

0
0

Are you actually looking to clear selected rows/cells? My understanding was that you wanted to clear the entire DataGrid. If so, you could do this:

myDataGrid.ItemsSource = null;

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.