Occurs when the System.Windows.Forms.DataGridView compares two cell values to perform a sort operation.
This event occurs only when the DataGridView.DataSource property is not set and the DataGridView.VirtualMode property value is false.
This event compares pairs of cells in the column being sorted. It occurs only when the user clicks the header of a column with a DataGridViewColumn.SortMode property value of DataGridViewColumnSortMode.Automatic, or when you call the DataGridView.Sort(DataGridViewColumn, System.ComponentModel.ListSortDirection) overload. When this event occurs for a column with a DataGridViewColumn.SortMode property value of DataGridViewColumnSortMode.Programmatic, you must display the sorting glyph yourself through the DataGridViewColumnHeaderCell.SortGlyphDirection property.
You can use this event to sort rows using the cell values in one column or in multiple columns. Use the DataGridViewSortCompareEventArgs.CellValue1 and DataGridViewSortCompareEventArgs.CellValue2 properties to compare cell values in the column specified in the DataGridViewSortCompareEventArgs.Column property. Use the DataGridViewSortCompareEventArgs.RowIndex1 and DataGridViewSortCompareEventArgs.RowIndex2 properties to access values in other columns through the DataGridView.Rows collection.
For more information about handling events, see Consuming Events.