Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit sorting silverlight datagrid
6 replies. Latest Post by yifung on July 2, 2009.
(0)
Nikotromus
Member
1 points
25 Posts
06-23-2009 5:58 PM |
How do I programatically sort a silverlight datagrid? My datagrid's item source is an observable collection. I would like to default my sort by the 5th field in the datagrid, and then be able to programatically sort based off of events.
Thanks in advance
gunjansh...
526 points
88 Posts
06-23-2009 6:38 PM |
If you use Silverlight 3 Beta, you could use SortDescriptions property on DataGrid to do this:
using System.ComponentModel;dataGrid.SortDescriptions.Add(new SortDescription("myProperty", ListSortDirection.Descending));
You can have multiple SortDescriptions and DataGrid will sort the columns in the order in which they appear in the SortDescription collection.
Gunjan [MSFT]
06-23-2009 6:46 PM |
Nuts. I'm using silverlight 2.
yifung
Contributor
3313 points
540 Posts
06-24-2009 6:59 PM |
Unfortunately, there isn't an easy way to do this in Silverlight 2. To get everything integrated with the sort glyphs, you'd bascially have to implement your one ICollectionView which I don't recommend. You could potentially take the public sources and rip out the CollectionView that the DataGrid uses. If you do that, I'd recommend replacing that as soon as a public CollectionView is introduced in Silverlight 3 so you get any updates and bug fixes.
SLMonster
299 points
65 Posts
06-25-2009 12:23 AM |
Hi,
<Style TargetType="prim:DataGridColumnHeader" x:Key="DataGridLinkColumnHeaderUnsorted" ><Setter Property="HorizontalContentAlignment" Value="Center" /><Setter Property="ContentTemplate"><Setter.Value> <DataTemplate> <StackPanel Orientation="Horizontal" > <HyperlinkButton Content="{Binding}" Tag="{Binding}" Click="Header_Click" ></HyperlinkButton></StackPanel> </DataTemplate></Setter.Value></Setter></Style>
Step 2 : define grid with CanUserSortColumns=false. and do not create columns as we are programmaticaly creating them
Setp3 : In code behind, Define below properties
public
Step4 : SetSortStyleMethodprivate void SetSortColumnStyle(DataGridColumn dataGridColumn){ if (CurrentSortColumn == dataGridColumn.Header.ToString()) { if (this.CurrentSortDirection == SortDirection.Ascending) dataGridColumn.HeaderStyle = this.Resources["DataGridLinkColumnHeaderUp"] as Style; else dataGridColumn.HeaderStyle = this.Resources["DataGridLinkColumnHeaderDown"] as Style; } else { //Unsorted dataGridColumn.HeaderStyle = this.Resources["DataGridLinkColumnHeaderUnsorted"] as Style; }}
Step 5 HEader click event handler to change the sort directionprivate void Header_Click(object sender, RoutedEventArgs e){ var link = sender as HyperlinkButton; if (link.Tag.ToString() == CurrentSortColumn){ ChangeCurentSortDirection();} else { CurrentSortColumn = link.Tag.ToString(); CurrentSortDirection = SortDirection.Ascending; } PopulateGrid();}
hope this helps
avtar
63 points
69 Posts
07-02-2009 2:07 AM |
Hi Gunjan,
How do I programatically sort a silverlight datagrid?
My datagrid's item source is an List<String> collection.
I am using Silverlight 3.
I cann't use
dg.SortDescriptions.Add(new SortDescription("myProperty", ListSortDirection.Ascending));
Because i don't have property, I have List<string>.
When user click on the column of datagrid following unhandled error pop up.
An unhandled exception ('Unhandled Error in silverlight Applicaiton SortDescription's property name 'COL - Asset' is invalid.at system.window.page.pagedCollectionView.RefreshOverrid()at System.Window.Data.PagedCollectionView.RefreshInternal()at System.Window.Data.PagedCollectionView.RefreshOnDefer();
Thanks lot.
07-02-2009 2:28 PM |
CollectionView sorting doesn't work on primitives types. This is true in WPF as well. You'll need to wrap your string in a object with a public string property