Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,
My problem is a little complex, but I have no idea how to solve it, so i try to ask you about this.
In WPF, my datagird column header is sortable. So, when i click once, the arrow appears and datagrid is sorted desc, then asc etc..

I need to have a sorted (with arrow visible) datagrid at start of my window. So I add SortDirection="Descending" to my DataGridTextColumn. Like this:

<DataGridTextColumn Binding="{Binding Path=SysTask}" Header="SysTask" Width="80" MinWidth="60" SortDirection="Descending"/>


Now, when I open my window, datagrid is sorted, the arrow is visible. But.. it's not ok. It is NOT THE SAME, as open the window without arrow, not sorted datagrid, and then click the header to sort. It's not the same, and i'm looking for the event - "click on the datagrid column header to sort".

I need, to click on column header programmatically from code. Is that possible?

I tell you why, it's not the same and why I need that event:

I have datagrid, which has to be sorted every couple of seconds. Datagrid could be big, so I do it like that: get new list, check if there are changes and then remove or add new records. Very simple..

But, when I do "SortDirection="Descending"", open window with already sorted datagrid with arrow etc... and when I add new record, it still appears on the end of the datagrid! Then when I click on header - it's moved for the right position. I have to click only once on column header, and when method add sth to list, it's on the right place. So I have to click on this header programmatically.

A second reason, I have a problem with refreshing.
When I call my refresh method from timer: fail. When I call it from button click: it works. When refresh method is called by timer, it fails, but when I click sort desc, click sort asc on header - it works. So I need that "click column header" event for that to.
Posted

1 solution

When it comes to sorting your DataGrid, the best thing is to use a CollectionView with a SortDescription. In XAML it would look something like this:-

XML
<Grid.Resources>
            <CollectionViewSource x:Key="DefaultSort" Source="{Binding yourSourceHere}">
                <CollectionViewSource.SortDescriptions>
                    <scm:SortDescription PropertyName="SysTask"/>
                </CollectionViewSource.SortDescriptions>
            </CollectionViewSource>
        </Grid.Resources>


(this assumes you have a namespace scm mapped to System.ComponentModel)

Then you can set up your grid like this:-

XML
<DataGrid ItemsSource="{Binding Source={StaticResource DefaultSort}}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Path=SysTask}" Header="SysTask" Width="80" SortDirection="Descending" />
            </DataGrid.Columns>
        </DataGrid>


Then whenever you add or subtract items from the grid, it will automagically sort everything according to that column Descending.

Hope this helps
 
Share this answer
 
v2
Comments
Simon Bang Terkildsen 8-Sep-11 10:06am    
+5 good answer
Wayne Gaylard 8-Sep-11 10:15am    
Thanks Simon
mnd017 8-Sep-11 10:29am    
What should I put in 'yourSourceHere'? Forgive me, I'm a beginner
mnd017 8-Sep-11 10:48am    
Well, It appears that's work. Before I've got < grid datacontext="{Binding Source={StaticResource _taskFactory}}">. Now I change it to put "_taskFactory" into "yourSourceHere".
But, another problem appears: I've got margin=-4 for my datagrid. And sometimes, when data changed, this one row has margin 0 (so it's moved to right a little bit). Could you suggest me why?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900