Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop Datagrid selecting first row by default

Tags:

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?

like image 554
viky Avatar asked Aug 19 '10 05:08

viky


People also ask

How to disable default row selection in datagridview?

You can set this property to null to temporarily remove the focus rectangle, but when the control receives focus and the value of this property is null, it is automatically set to the value of the FirstDisplayedCell property. So looks like setting it to null only works if it wasn't the first row first column cell.

What is the default event of DataGrid?

By default, the DataGrid control generates columns automatically when you set the ItemsSource property. The type of column that is generated depends on the type of data in the column.

What is WPF DataGrid?

A DataGrid is a control that displays data in a customizable grid. It provides a flexible way to display a collection of data in rows and columns.


2 Answers

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.

like image 198
HCL Avatar answered Oct 13 '22 23:10

HCL


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...

like image 40
Valentin Avatar answered Oct 13 '22 22:10

Valentin