viky
viky

Reputation: 17689

Stop Datagrid selecting first row by default

I am using Wpf Toolkit DataGrid. Whenever I assign Itemssource to it, its first item get selected and its selectionChanged event gets called. How can I stop it to select any row by default?

Upvotes: 21

Views: 18970

Answers (4)

HCL
HCL

Reputation: 36815

Check if you have set IsSynchronizedWithCurrentItem="True" and you require it to be set alike?

<DataGrid IsSynchronizedWithCurrentItem="True" ... 
            
            

With set this property to true, the selection of the first item is the default-behaviour.

Upvotes: 42

Benoit Dufresne
Benoit Dufresne

Reputation: 343

HCL's answer is correct, but for fast and loose readers such as me, it proved confusing and I ended up spending some more time looking around investigating other things before coming back here and reading carefully.

<DataGrid IsSynchronizedWithCurrentItem="False" ... 

Is the bit we're interested in, not its antagonist!

To add some value of my own: the property IsSynchronizedWithCurrentItem=True means the grid's CurrentItem will be synchronized with the collection's current item. Setting IsSynchronizedWithCurrentItem=False is what we want here.

For Xceed's Datagrid users (such as I was in this case), that'll be SynchronizeCurrent=False

Upvotes: 8

Mageician
Mageician

Reputation: 2927

I tried a number of different things but what worked for me was to capture the first selection event and "undo" it by unselecting all on the datagrid.

Here's the code to make this work, I hope it's beneficial to someone else :)

/* Add this inside your window constructor */
this.myDataGrid.SelectionChanged += myDataGrid_SelectionChanged;

/* Add a private boolean variable for saving the suppression flag */
private bool _myDataGrid_suppressed_flag = false;

/* Add the selection changed event handler */
void myDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    /* I check the sender type just in case */
    if (sender is System.Windows.Controls.DataGrid)
    {
         System.Windows.Controls.DataGrid _dg = (System.Windows.Controls.DataGrid)sender;

        /* If the current item is null, this is the initial selection event */
         if (_dg.CurrentItem == null)
         {
              if (!_myDataGrid_suppressed_flag)
              {
                    /* Set your suppressed flat */
                    _dgRateList_suppressed_flag = true;
                    /* Unselect all */
                    /* This will trigger another changed event where CurrentItem == null */
                    _dg.UnselectAll();

                    e.Handled = true;
                    return;
              }
         }
         else
         {
                /* This is a legitimate selection changed due to user interaction */
         }
    }
}

Upvotes: 1

Valentin
Valentin

Reputation: 782

Chances are that your DataGrid is bound to a collection like PagedCollectionView that has a CurrentItem property. This property is auto-synchronized with the selected row, in both directions. The solution would be to set the CurrentItem to null. You can do it like this:

PagedCollectionView pcv = new PagedCollectionView(collection);
pcv.MoveCurrentTo(null);
dataGrid.ItemsSource = pcv;

This is especially helpful in Silverlight, which has no DataGrid.IsSynchronizedWithCurrentItem property...

Upvotes: 12

Related Questions