Permalink
Fetching contributors…
Cannot retrieve contributors at this time
59 lines (47 sloc) 5.16 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
How to: Customize Sorting in the Windows Forms DataGridView Control
03/30/2017
.net-framework
dotnet-winforms
article
jsharp
sorting, DataGridView control
DataGridView control [Windows Forms], sorting
data grids, customizing sorting
92fb5c14-afab-4cf5-a97e-924fd9cb99f5
12
dotnet-bot
dotnetcontent
wpickett

How to: Customize Sorting in the Windows Forms DataGridView Control

The xref:System.Windows.Forms.DataGridView control provides automatic sorting but, depending on your needs, you might need to customize sort operations. For example, you can use programmatic sorting to create an alternate user interface (UI). Alternatively, you can handle the xref:System.Windows.Forms.DataGridView.SortCompare event or call the Sort(IComparer) overload of the xref:System.Windows.Forms.DataGridView.Sort%2A method for greater sorting flexibility, such as sorting multiple columns.

The following code examples demonstrate these three approaches to custom sorting. For more information, see Column Sort Modes in the Windows Forms DataGridView Control.

Programmatic Sorting

The following code example demonstrates a programmatic sort using the xref:System.Windows.Forms.DataGridView.SortOrder%2A and xref:System.Windows.Forms.DataGridView.SortedColumn%2A properties to determine the direction of the sort, and the xref:System.Windows.Forms.DataGridViewColumnHeaderCell.SortGlyphDirection%2A property to manually set the sort glyph. The Sort(DataGridViewColumn,ListSortDirection) overload of the xref:System.Windows.Forms.DataGridView.Sort%2A method is used to sort data only in a single column.

[!code-csharpSystem.Windows.Forms.DataGridViewProgrammaticSort#00] [!code-vbSystem.Windows.Forms.DataGridViewProgrammaticSort#00]

Custom Sorting Using the SortCompare Event

The following code example demonstrates custom sorting using a xref:System.Windows.Forms.DataGridView.SortCompare event handler. The selected xref:System.Windows.Forms.DataGridViewColumn is sorted and, if there are duplicate values in the column, the ID column is used to determine the final order.

[!code-csharpSystem.Windows.Forms.DataGridView.SortCompare#00] [!code-vbSystem.Windows.Forms.DataGridView.SortCompare#00]

Custom Sorting Using the IComparer Interface

The following code example demonstrates custom sorting using the Sort(IComparer) overload of the xref:System.Windows.Forms.DataGridView.Sort%2A method, which takes an implementation of the xref:System.Collections.IComparer interface to perform a multiple-column sort.

[!code-csharpSystem.Windows.Forms.DataGridViewIComparerSort#00] [!code-vbSystem.Windows.Forms.DataGridViewIComparerSort#00]

Compiling the Code

These examples require:

  • References to the System, System.Drawing, and System.Windows.Forms assemblies.

For information about building these examples from the command line for [!INCLUDEvbprvb] or [!INCLUDEcsprcs], see Building from the Command Line or Command-line Building With csc.exe. You can also build this example in [!INCLUDEvsprvs] by pasting the code into a new project. Also see How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio.

See Also

xref:System.Windows.Forms.DataGridView
Sorting Data in the Windows Forms DataGridView Control
Column Sort Modes in the Windows Forms DataGridView Control
How to: Set the Sort Modes for Columns in the Windows Forms DataGridView Control