Permalink
Fetching contributors…
Cannot retrieve contributors at this time
51 lines (41 sloc) 3.11 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: Set the Sort Modes for Columns in the Windows Forms DataGridView Control
03/30/2017
.net-framework
dotnet-winforms
article
jsharp
sorting, data grids
DataGridView control [Windows Forms], sort mode
data grids, sorting data
57dfed60-a608-40d5-86f9-d65686ffb325
14
dotnet-bot
dotnetcontent
wpickett

How to: Set the Sort Modes for Columns in the Windows Forms DataGridView Control

In the xref:System.Windows.Forms.DataGridView control, text box columns use automatic sorting by default, while other column types are not sorted automatically. Sometimes you will want to override these defaults. For example, you can display images in place of text, numbers, or enumeration cell values. While the images cannot be sorted, the underlying values that they represent can be sorted.

In the xref:System.Windows.Forms.DataGridView control, the xref:System.Windows.Forms.DataGridViewColumn.SortMode%2A property value of a column determines its sorting behavior.

The following procedure shows the Priority column from How to: Customize Data Formatting in the Windows Forms DataGridView Control. This column is an image column and is not sortable by default. It contains actual cell values that are strings, however, so it can be sorted automatically.

To set the sort mode for a column

Compiling the Code

This example requires:

  • A xref:System.Windows.Forms.DataGridView control named dataGridView1 that contains a column named Priority.

  • References to the xref:System?displayProperty=nameWithType and xref:System.Windows.Forms?displayProperty=nameWithType assemblies.

See Also

xref:System.Windows.Forms.DataGridView
xref:System.Windows.Forms.DataGridViewColumn.SortMode%2A?displayProperty=nameWithType
Sorting Data in the Windows Forms DataGridView Control
Column Sort Modes in the Windows Forms DataGridView Control
How to: Customize Sorting in the Windows Forms DataGridView Control