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. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to programatically sort the columns in GridGroupingControl?

Platform: ASP.NET Web Forms (Classic) |
Control: GridGroupingControl |
Published Date: April 30, 2011 |
Last Revised Date: July 10, 2014

 

To sort the columns programatically in GridGroupingControl the particular column should be added into the collection of SortedColumns. Please refer the below code snippet which illustrates this:

C#

//Including the direction of sorting

this.GridGroupingControl1.TableDescriptor.SortedColumns.Add("CategoryID", System.ComponentModel.ListSortDirection.Descending);

//this.GridGroupingControl1.TableDescriptor.SortedColumns.Add("CategoryID");

VB

'Including the direction of sorting

Me.GridGroupingControl1.TableDescriptor.SortedColumns.Add("CategoryID",System.ComponentModel.ListSortDirection.Descending)

'Me.GridGroupingControl1.TableDescriptor.SortedColumns.Add("CategoryID")

Sample:

http://help.syncfusion.com/support/samples/kb/Grid.Web/6.1.0.34/GGCCodeSort/CodeSort.zip

ADD COMMENT
You must log in to leave a comment
Comments
QLT User
Apr 19, 2016
how do i get the sort direction of a column when the header column is clicked ? i would like to save it and apply it Reply
QLT User
Apr 19, 2016
tried this but it always returns ascending void gridGroupingControl1_TableControlCellClick(object sender, GridTableControlCellClickEventArgs e) { int rowIndex = e.Inner.RowIndex; int colIndex = e.Inner.ColIndex; GridGroupingControl grid = sender as GridGroupingControl; GridTableCellStyleInfo style = grid.TableControl.GetTableViewStyleInfo(rowIndex, colIndex); string clickedCol = style.TableCellIdentity.Column.MappingName; if (e.Inner.MouseEventArgs.Button == System.Windows.Forms.MouseButtons.Right) { if (style.TableCellIdentity.TableCellType == GridTableCellType.ColumnHeaderCell) { } } else { #region LeftMouseClick Behaviour if (style.TableCellIdentity.TableCellType == GridTableCellType.ColumnHeaderCell) { #region Header Cell grid.Engine.TableDescriptor.SortedColumns.Clear(); grid.Engine.TableDescriptor.SortedColumns.Reset(); _isAscendingSort = !_isAscendingSort; if (_isAscendingSort) { grid.Engine.TableDescriptor.SortedColumns.Add(clickedCol, ListSortDirection.Ascending); } else { grid.Engine.TableDescriptor.SortedColumns.Add(clickedCol, ListSortDirection.Descending); } //// Gets the sort order. Syncfusion.Grouping.SortColumnDescriptor sort = new Syncfusion.Grouping.SortColumnDescriptor(clickedCol); System.ComponentModel.ListSortDirection lst = sort.SortDirection; // Displays the order which the column is being sorted. Console.WriteLine("FTL_DEBUG: The sort = {0}:{1} Ascending = {2}", clickedCol, lst.ToString(), _isAscendingSort); #endregion } else if (style.TableCellIdentity.TableCellType == GridTableCellType.RecordFieldCell) { int fld = grid.TableDescriptor.ColIndexToField(colIndex); switch (clickedCol) { case "ShipName": string shipCountry = grid.TableModel[rowIndex, _ShipCountryIndex].CellValue.ToString(); String shipName = Convert.ToString(grid.TableModel[rowIndex, _ShipNameIndex].CellValue); //Do Something if (shipCountry == "USA") { } break; default: break; } } #endregion } } Reply
Maciej Kromrych
Jan 09, 2018

It works as long as I keep clicking the "right" column header (the one that has the SortColumnDescriptor with custom IComparer). But after I click some other header, and then again the header with custom SortColumnDescriptor/IComparer, I get default sort, not my custom sort :(

GridGroupingControl grid = ...

grid.DataSource = ...

SortColumnDescriptor d = new SortColumnDescriptor("Code", ListSortDirection.Ascending);

d.Comparer = new Icd9CodeComparer(); // <--- custom comparer

grid.TableDescriptor.SortedColumns.Add(d);

Reply
Maciej Kromrych
Jan 09, 2018

OK, found out how to do it properly:

grid.TableDescriptor.SortedColumns.Changing += (src, args) => {

     if (args.Action == Syncfusion.Collections.ListPropertyChangedType.Add) {

          SortColumnDescriptor d = args.Item as SortColumnDescriptor;

          if (d?.Name == "Code" && d.Comparer == null)

               d.Comparer = new Icd9CodeComparer();    // <-- comparer is gone after sorting by other columns; restore it

     }};

grid.TableDescriptor.SortedColumns.Add("Code");     // <-- force initial sort

Reply

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon