We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Sort by a column - problem

Hi:

In my grouped grid i want to keep the data to be always sorted by one of the columns, say, "IsNew" column. Then I'd like allow user to sort by other columns as they wish (the "IsNew" column is hidden). However I found whenever user clicks on the column header to change sorting, the SortedColumns collection is cleared. To fix this, I tried to handle TableDescriptor.SortedColumns.Changed event and add the column back:

void SortedColumns_Changed(object sender, ListPropertyChangedEventArgs e)
{
if (!this.grid.TableDescriptor.SortedColumns.Contains("IsNew"))
this.grid.TableDescriptor.SortedColumns.Insert(0, new SortColumnDescriptor("IsNew", ListSortDirection.Ascending));
}

After this change I found when i click on any colum header to sort the data, the colum keeps in ascending order. I trace down the event and found out that no matter how many times i click the column header the ListSortDirection is alwasy Ascending, instead of flipping between ascending/descending as before. Any ideas?

2 Replies

HA haneefm Syncfusion Team July 10, 2007 07:57 PM UTC

Hi Stanley,

This can be achieved by handling a the TableControlQueryAllowSortColumn event handler and set the e.AllowSort property to false to cancel default grid sorting for the required columns. Please try the following code snippet and let me know if this helps

void gridGroupingControl1_TableControlQueryAllowSortColumn(object sender, GridQueryAllowSortColumnEventArgs e)
{
e.AllowSort = false;
ListSortDirection direction = ListSortDirection.Ascending;
if (this.gridGroupingControl1.TableDescriptor.SortedColumns.Contains(e.Column.Name))
{
direction = this.gridGroupingControl1.TableDescriptor.SortedColumns[e.Column.Name].SortDirection;
direction = direction == ListSortDirection.Descending ? ListSortDirection.Ascending : ListSortDirection.Descending;
}
this.gridGroupingControl1.TableDescriptor.SortedColumns.Clear();
this.gridGroupingControl1.TableDescriptor.SortedColumns.Add("IsNew");
if (e.Column.Name != "IsNew")
this.gridGroupingControl1.TableDescriptor.SortedColumns.Add(e.Column.Name, direction);
}

Best Regards,
Haneef


HA Haishi July 10, 2007 09:01 PM UTC

Haneef:

Worked like a charm! Thanks a lot!

Stanley

Loader.
Live Chat Icon For mobile
Up arrow icon