Sorts the contents of the DataGridView control in ascending or descending order based on the contents of the specified column.
Namespace:
System.Windows.Forms
Assembly:
System.Windows.Forms (in System.Windows.Forms.dll)
Visual Basic (Declaration)
Public Overridable Sub Sort ( _
dataGridViewColumn As DataGridViewColumn, _
direction As ListSortDirection _
)
Dim instance As DataGridView
Dim dataGridViewColumn As DataGridViewColumn
Dim direction As ListSortDirection
instance.Sort(dataGridViewColumn, direction)
public virtual void Sort(
DataGridViewColumn dataGridViewColumn,
ListSortDirection direction
)
public:
virtual void Sort(
DataGridViewColumn^ dataGridViewColumn,
ListSortDirection direction
)
public function Sort(
dataGridViewColumn : DataGridViewColumn,
direction : ListSortDirection
)
This method sorts the contents of the DataGridView by comparing values in the specified column. By default, the sort operation will use the Compare method to compare pairs of cells in the column using the DataGridViewCell..::.Value property.
For columns with the SortMode property set to DataGridViewColumnSortMode..::.Automatic, the SortedColumn and SortOrder properties are set automatically and the appropriate sorting glyph is displayed. For columns with the SortMode property set to DataGridViewColumnSortMode..::.Programmatic, you must display the sorting glyph yourself through the DataGridViewColumnHeaderCell..::.SortGlyphDirection property.
You can customize the sorting operation used by this method by handling the SortCompare event. This event occurs only when the DataSource property has not been set.
When the DataSource property has been set, this method works for data-bound columns only. Data-bound columns have had their DataGridViewColumn..::.DataPropertyName property set. This causes the DataGridViewColumn..::.IsDataBound property to return true.
If your DataGridView control contains both bound and unbound columns, you must implement virtual mode to maintain the values of the unbound columns when the control is sorted by a bound column. You can do this by setting the VirtualMode property to true and handling the CellValueNeeded event. If the column is editable, you should also handle the CellValuePushed event. For more information about virtual mode, see How to: Implement Virtual Mode in the Windows Forms DataGridView Control. Sorting by unbound columns when the control is data-bound is not supported.
The following code example demonstrates how to use the Sort in a programmatic sort.
Private Sub SortButton_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles sortButton.Click
' Check which column is selected, otherwise set NewColumn to Nothing.
Dim newColumn As DataGridViewColumn
If dataGridView1.Columns.GetColumnCount(DataGridViewElementStates _
.Selected) = 1 Then
newColumn = dataGridView1.SelectedColumns(0)
Else
newColumn = Nothing
End If
Dim oldColumn As DataGridViewColumn = dataGridView1.SortedColumn
Dim direction As ListSortDirection
' If oldColumn is null, then the DataGridView is not currently sorted.
If oldColumn IsNot Nothing Then
' Sort the same column again, reversing the SortOrder.
If oldColumn Is newColumn AndAlso dataGridView1.SortOrder = _
SortOrder.Ascending Then
direction = ListSortDirection.Descending
Else
' Sort a new column and remove the old SortGlyph.
direction = ListSortDirection.Ascending
oldColumn.HeaderCell.SortGlyphDirection = SortOrder.None
End If
Else
direction = ListSortDirection.Ascending
End If
' If no column has been selected, display an error dialog box.
If newColumn Is Nothing Then
MessageBox.Show("Select a single column and try again.", _
"Error: Invalid Selection", MessageBoxButtons.OK, _
MessageBoxIcon.Error)
Else
dataGridView1.Sort(newColumn, direction)
If direction = ListSortDirection.Ascending Then
newColumn.HeaderCell.SortGlyphDirection = SortOrder.Ascending
Else
newColumn.HeaderCell.SortGlyphDirection = SortOrder.Descending
End If
End If
End Sub
private void sortButton_Click(object sender, System.EventArgs e)
{
// Check which column is selected, otherwise set NewColumn to null.
DataGridViewColumn newColumn =
dataGridView1.Columns.GetColumnCount(
DataGridViewElementStates.Selected) == 1 ?
dataGridView1.SelectedColumns[0] : null;
DataGridViewColumn oldColumn = dataGridView1.SortedColumn;
ListSortDirection direction;
// If oldColumn is null, then the DataGridView is not currently sorted.
if (oldColumn != null)
{
// Sort the same column again, reversing the SortOrder.
if (oldColumn == newColumn &&
dataGridView1.SortOrder == SortOrder.Ascending)
{
direction = ListSortDirection.Descending;
}
else
{
// Sort a new column and remove the old SortGlyph.
direction = ListSortDirection.Ascending;
oldColumn.HeaderCell.SortGlyphDirection = SortOrder.None;
}
}
else
{
direction = ListSortDirection.Ascending;
}
// If no column has been selected, display an error dialog box.
if (newColumn == null)
{
MessageBox.Show("Select a single column and try again.",
"Error: Invalid Selection", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else
{
dataGridView1.Sort(newColumn, direction);
newColumn.HeaderCell.SortGlyphDirection =
direction == ListSortDirection.Ascending ?
SortOrder.Ascending : SortOrder.Descending;
}
}
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0
Reference
Other Resources