$99 VS $1000+. UI Control for ASP.NET AJAX(contains chart and gridview).
GridView Control for Windows Forms
Set data sorting in grid by using DataGridView component in Windows Forms applications
Home > WinForms UI Controls > User Manual > Expressions of Grid Sorting in C#

Custom Sorting in GridView

The Kettic GridView component is able to create custom sorting of grid data. This flexible sorting allows users to sort rows data that is bound to GridView according to custom logic. To create custom sorting to grid data, we need enable the GridView.EnableSorting property or the GridViewTemplate.EnableSorting property. When enabling the sorting property, the GridView will sort data at all levels. There are two approaches available for users to customize sorting to GridView, handling the CustomizeSorting event and applying a custom SortComparer to replace the Kettic GridView sorting.

How to Create Custom Sorting to GridView

When we enable the GridView.EnableSorting property or the GridViewTemplate.EnableSorting property, the CustomSorting event is fired. This custom sorting event contains several properties, such as Template, Row1, Row2, SortResult, and Handled. The following are the explanation of these properties.
  • Template, this property is used to sort the grid rows held by the template
  • Row1, Row2, the properties will determine the two rows that will be compared
  • SortResult, the property is able to get back the negative value if Row1 is before Row2, positive value when Row1 is after Row2, and zero when the value of the rows are equaled to each other in a specified column.
  • Handled, this property is used to define if the comparison of the two grid data rows is processed by the custom algorithm or by the applied sort descriptors.

C# code for Creating Custom Sorting

The C# code below shows how to handle the CustomSorting event to perform the ascending grid data sorting based on the Price column in GridView rows.

this.ketticGridView1.EnableCustomSorting = true;
this.ketticGridView1.CustomSorting += new GridViewCustomSortingEventHandler(ketticGridView1_CustomSorting);
this.ketticGridView1.Columns["Price"].SortOrder = KetticSortOrder.Ascending;

private void ketticGridView1_CustomSorting(object sender, GridViewCustomSortingEventArgs e)
{
decimal row1Price = (decimal)e.Row1.Cells["Price"].Value;
decimal row2Price = (decimal)e.Row2.Cells["Price"].Value;
if (row1Price > row2Price)
{
e.SortResult = 1;
}
else if (row1Price < row2Price)
{
e.SortResult = -1;
}
else
{
e.SortResult = 0;
}
}
The C# code below shows how to use the Handled property of the CustomSorting event arguments. In this case, we are going to uses custom sorting to sort the grid data rows descending according to the Price column values.

this.ketticGridView1.EnableCustomSorting = true;
this.ketticGridView1.CustomSorting += new GridViewCustomSortingEventHandler(ketticGridView1_CustomSorting);

SortDescriptor sortDescriptor = new SortDescriptor("Price", ListSortDirection.Descending);
this.ketticGridView1.SortDescriptors.Add(sortDescriptor);

private void ketticGridView1_CustomSorting(object sender, GridViewCustomSortingEventArgs e)
{
decimal row1Price = (decimal)e.Row1.Cells["Price"].Value;
decimal row2Price = (decimal)e.Row2.Cells["Price"].Value;

if (row1Price < 0.55m || row2Price < 0.55m)
{
e.Handled = false;
return;
}

if (row1Price > row2Price)
{
e.SortResult = 1;
}
else if (row1Price < row2Price)
{
e.SortResult = -1;
}
else
{
e.SortResult = 0;
}
}

Implement Data Sorting using SortComparer in C#.NET

To implement data sorting by using the SortComparer in C#.NET project, we need use a custom sorting to replace the default one. This is accomplished through setting the SortComparer of the GridViewTemplate. The C# code below shows how to sort the grid data rows ascending according to the name of the Product by a custom sorting logic.
The following example demonstrates how to use a custom sorting mechanism in KetticGridView to sort the KetticGridView rows Descending by the length of the ShipCity column:

this.ketticGridView1.Columns["Product"].SortOrder = KetticSortOrder.Descending;

this.ketticGridView1.MasterTemplate.SortComparer = new CustomComparer();

public class CustomComparer : IComparer<GridViewRowInformation>
{
public int Compare(GridViewRowInformation x, GridViewRowInformation y)
{
int row1ProductName = x.Cells["ProductName"].Value.ToString().Name;
int row2ProductName = y.Cells["ProductName"].Value.ToString().Name;

int result = 0;
if (row1ProductName > row2ProductName)
{
result = 1;
}
else if (row1ProductName < row2ProductName)
{
result = -1;
}

return result;
}
}
UI Controlsfor Windows Forms
.NET WinForms UI Overview.NET WinForms UI Features.NET WinForms UI GuideC# WinForms UI DesignVB.NET WinForms UI Design
WinForms UI Controls