I am working on WPF application using MVVM model ,I am using DataGrid which is having 3 columns and second column is datatime column.In DataGrid I want to display last added record as first row means in descending order so I am using CollectionViewSource and sort the data based on second column. Here the problem is when the grid rows increases then scrollbar will come automatically but it is not going upwards so first row is not displaying and everytime after inserting new record I want to scroll the scrollbar to top manually and it is annoying to customer also
below is the code,
var collectionView = CollectionViewSource.GetDefaultView(this.GridDataSource);
collectionView.SortDescriptions.Add(new SortDescription("CreatedAt", ListSortDirection.Descending));
collectionView.Refresh();
In XAML, inside DataGrid I used selectedIndex=0, this will works when we launch the application but after inserting new records the selected row is not changing but here I want to selected the newly inserted row,
<DataGrid AutoGenerateColumns="False" SelectedIndex="0"
ItemsSource="{Binding Path=Commands}" >
I tried added a property and bind to selectedIndex and still the same issue but when I debugged the code I found that selectedIdex vaule is changing means 0 to 1 and 1 to 2 soon after adding new records but in grid these added data is there and i need to manually scroll up to see the added rows.
<DataGrid SelectedIndex="{Binding selectedRow}" >
After executing below code the SelectedIndex value means selectedRow value changing from 0 to 1 and so on
this.GridDataSource.Add("FirstColumn",datetime.now,"Thirdcolumn");
In Simple I want to select FirstRow of the Grid.
Thanks in advance
You need to clear the previous sort, or you're potentially just adding more and more sort descriptions.
You can manipulate the selected item indirectly, via the collectionview. In fact you might have to in order to get at the first of the sorted items. On your datagrid:
You can then select the first one using collectionview:
Because of that setting on the datagrid that will try and make the first item selected. Except of course it can be virtualised so there's no UI there to select. You need to call scrollintoview.
I use a behavior:
Since that's applied to the datagrid you can put it just inside the datagrid tag:
Behaviors are now a nuget package which you need to add and the xmlns is different:
https://github.com/Microsoft/XamlBehaviorsWpf
Selecting a row doesn't turn it blue. You might be interested in another behavior
Obviously, this is applied to the datagridrow