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
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to Support Tri-State Sorting in a GridDataBoundGridControl?

Platform: WinForms |
Control: GridDataBoundGrid(Classic)

 

The approach taken in this article is to extend the standard sorting by allowing the user to click on the column headers to change the sorting that is applied to the GridDataBoundGridControl. The standard sorting never removes the sorting from a column; so once a column is sorted it will either be in ascending or descending order. By using the GridDataBoundGridControl’s CellClick event, it is possible to alter this behavior so that when a column is clicked once, it will be sorted in an ascending order, when it is clicked a second time, the column will be sorted in a descending order and when the column is clicked a third time, it will have its sorting removed.

The tri-state sort behavior is accomplished by storing the sort indicator which, is in the Tag property of the column and by handling the CellClick event. When a column is sorted in descending order, then the sorting is removed the next time the column is clicked.

1)There are a couple of problems that you will have to work around. One is that the standard column header cell uses the Tag for its sortheader and it explicitly sets the non-sorted header's Tag to ascending /descending only .It does not support the none option for unsorting the GridDataBoundGridControl. So, you need to hide the HeaderCell and add the New Header.

C#

 
this.gridDataBoundGrid1.DataSource = dt;
this.gridDataBoundGrid1.Model.BaseStylesMap["Column Header"].StyleInfo.CellType = "Header";
this.gridDataBoundGrid1.Model.Data.RowCount = 1;
this.gridDataBoundGrid1.Model.Rows.HeaderCount = 1;
this.gridDataBoundGrid1.Model.Rows.FrozenCount = 1;
this.gridDataBoundGrid1.Model.HideRows[0] = true;
for(int col = 1; col <= 3; ++col)
{
this.gridDataBoundGrid1[1, col] = this.gridDataBoundGrid1[0,1] ;
this.gridDataBoundGrid1[1, col].CellType = "ColumnHeaderCell";
}
 

 

VB

 
Me.gridDataBoundGrid1.DataSource = dt
Me.gridDataBoundGrid1.Model.BaseStylesMap("Column Header").StyleInfo.CellType = "Header"
Me.gridDataBoundGrid1.Model.Data.RowCount = 1
Me.gridDataBoundGrid1.Model.Rows.HeaderCount = 1
Me.gridDataBoundGrid1.Model.Rows.FrozenCount = 1
Me.gridDataBoundGrid1.Model.HideRows(0) = True
Dim col As Integer
For col = 1 To 3 Step + 1
Me.gridDataBoundGrid1(1, col) = Me.gridDataBoundGrid1(0,1)
Me.gridDataBoundGrid1(1, col).CellType = "ColumnHeaderCell"
Next
 

 

2)In Cellclick Event perform the sorting

C#

private void gridDataBoundGrid1_CellClick(object sender, Syncfusion.Windows.Forms.Grid.GridCellClickEventArgs e)
{
if(this.gridDataBoundGrid1[e.RowIndex,e.ColIndex].CellType == "ColumnHeaderCell")
{
if(this.gridDataBoundGrid1[e.RowIndex,e.ColIndex].HasTag )
{
if(this.gridDataBoundGrid1[e.RowIndex,e.ColIndex].Tag.ToString() == "None")
{
this.gridDataBoundGrid1[e.RowIndex,e.ColIndex].Tag = ListSortDirection.Ascending;
SortColumn(e.RowIndex,e.ColIndex);
}
else if((ListSortDirection )this.gridDataBoundGrid1[e.RowIndex, e.ColIndex].Tag == ListSortDirection.Ascending)
{
this.gridDataBoundGrid1[e.RowIndex,e.ColIndex].Tag = ListSortDirection.Descending ;
SortColumn(e.RowIndex,e.ColIndex);
}
else if((ListSortDirection)this.gridDataBoundGrid1[e.RowIndex, e.ColIndex].Tag == ListSortDirection.Descending)
{
this.gridDataBoundGrid1[e.RowIndex,e.ColIndex].Tag = "None";
SortColumn(e.RowIndex,e.ColIndex);
}
}
else
{
this.gridDataBoundGrid1[e.RowIndex,e.ColIndex].Tag = ListSortDirection.Ascending ;
SortColumn(e.RowIndex,e.ColIndex);
}
}
}
 

 

VB

Private Sub gridDataBoundGrid1_CellClick(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.GridCellClickEventArgs)
If Me.gridDataBoundGrid1(e.RowIndex,e.ColIndex).CellType = "ColumnHeaderCell" Then
If Me.gridDataBoundGrid1(e.RowIndex,e.ColIndex).HasTag Then
If Me.gridDataBoundGrid1(e.RowIndex,e.ColIndex).Tag.ToString() = "None" Then
Me.gridDataBoundGrid1(e.RowIndex,e.ColIndex).Tag = ListSortDirection.Ascending
SortColumn(e.RowIndex,e.ColIndex)
Else If CType(Me.gridDataBoundGrid1(e.RowIndex,e.ColIndex).Tag = ListSortDirection.Ascending,ListSortDirection) Then
Me.gridDataBoundGrid1(e.RowIndex,e.ColIndex).Tag = ListSortDirection.Descending
SortColumn(e.RowIndex,e.ColIndex)
Else If CType(Me.gridDataBoundGrid1(e.RowIndex,e.ColIndex).Tag = ListSortDirection.Descending,ListSortDirection) Then
Me.gridDataBoundGrid1(e.RowIndex,e.ColIndex).Tag = "None"
SortColumn(e.RowIndex,e.ColIndex)
End If
Else
Me.gridDataBoundGrid1(e.RowIndex,e.ColIndex).Tag = ListSortDirection.Ascending
SortColumn(e.RowIndex,e.ColIndex)
End If
End If
End Sub
 

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

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

Up arrow icon

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

Live Chat Icon For mobile