WPF DataGrid: How to clear selection programmatically?
Asked Answered
T

5

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.

Titration answered 7/10, 2010 at 3:46 Comment(0)
S
26
dataGrid.UnselectAll()

For rows mode

Stenopetalous answered 1/8, 2013 at 8:52 Comment(0)
E
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
Ebbie answered 7/10, 2010 at 6:51 Comment(8)
@miliu Yes, my previous code wasn't correct. I fixed my answer.Ebbie
@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.Titration
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()));"Titration
Just a note...WPF DataGrid is great, but a few minor things drive me crazy.Titration
@miliu Dispatcher is used if it is necessary to use a wpf element from a thread which isn't UI thread. But threads in the event handler look strange. Anyway I haven't seen your complete code, so the Dispatcher class might be necessary.Ebbie
at the Silverlight Datagrid, if you don't use Extended Selection mode (multi-selection) it seems you can't do grid.SelectedItems.Clear() but have to do grid.SelectedItem=null instead. Maybe something similar is true in the WPF one?Stadiometer
@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.Ebbie
yeah, seems they use the same strange patternStadiometer
C
2
DataGrid.UnselectAllCells()

It works for me.

Crow answered 11/1, 2013 at 17:16 Comment(0)
M
1

Disabling and reenabling the DataGrid worked for me.

Manganite answered 16/3, 2012 at 18:50 Comment(0)
R
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;
Rarefaction answered 26/11, 2021 at 8:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.