DataGridView.Sort Method (DataGridViewColumn, ListSortDirection)
Sorts the contents of the DataGridView control in ascending or descending order based on the contents of the specified column.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
abstract Sort : dataGridViewColumn:DataGridViewColumn * direction:ListSortDirection -> unit override Sort : dataGridViewColumn:DataGridViewColumn * direction:ListSortDirection -> unit
dataGridViewColumn is null.
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.
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.
Available since 2.0