DataGridViewSortCompareEventArgs Class

Definition

Provides data for the SortCompare event.

public ref class DataGridViewSortCompareEventArgs : System::ComponentModel::HandledEventArgs
public class DataGridViewSortCompareEventArgs : System.ComponentModel.HandledEventArgs
type DataGridViewSortCompareEventArgs = class
    inherit HandledEventArgs
Public Class DataGridViewSortCompareEventArgs
Inherits HandledEventArgs
Inheritance
DataGridViewSortCompareEventArgs

Examples

The following code example demonstrates the use of SortCompare in a multiple column sort. This example is part of a larger example provided in How to: Customize Sorting in the Windows Forms DataGridView Control.

private void dataGridView1_SortCompare(object sender,
    DataGridViewSortCompareEventArgs e)
{
    // Try to sort based on the cells in the current column.
    e.SortResult = System.String.Compare(
        e.CellValue1.ToString(), e.CellValue2.ToString());

    // If the cells are equal, sort based on the ID column.
    if (e.SortResult == 0 && e.Column.Name != "ID")
    {
        e.SortResult = System.String.Compare(
            dataGridView1.Rows[e.RowIndex1].Cells["ID"].Value.ToString(),
            dataGridView1.Rows[e.RowIndex2].Cells["ID"].Value.ToString());
    }
    e.Handled = true;
}
Private Sub DataGridView1_SortCompare( _
    ByVal sender As Object, ByVal e As DataGridViewSortCompareEventArgs) _
    Handles DataGridView1.SortCompare

    ' Try to sort based on the contents of the cell in the current column.
    e.SortResult = System.String.Compare(e.CellValue1.ToString(), _
        e.CellValue2.ToString())

    ' If the cells are equal, sort based on the ID column.
    If (e.SortResult = 0) AndAlso Not (e.Column.Name = "ID") Then
        e.SortResult = System.String.Compare( _
            DataGridView1.Rows(e.RowIndex1).Cells("ID").Value.ToString(), _
            DataGridView1.Rows(e.RowIndex2).Cells("ID").Value.ToString())
    End If

    e.Handled = True

End Sub

Remarks

You can handle the SortCompare event to provide custom sorting. This event occurs only when the DataGridView.DataSource property is not set and the DataGridView.VirtualMode property is set to false. This event occurs once for each pair 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 Automatic, or when you call the DataGridView.Sort(DataGridViewColumn, ListSortDirection) overload. When this event occurs for a column with a DataGridViewColumn.SortMode property value of 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 CellValue1 and CellValue2 properties to compare cell values in the column specified in the Column property. Use the RowIndex1 and RowIndex2 properties to access values in other columns through the DataGridView.Rows collection.

Constructors

DataGridViewSortCompareEventArgs(DataGridViewColumn, Object, Object, Int32, Int32)

Initializes a new instance of the DataGridViewSortCompareEventArgs class.

Properties

CellValue1

Gets the value of the first cell to compare.

CellValue2

Gets the value of the second cell to compare.

Column

Gets the column being sorted.

Handled

Gets or sets a value that indicates whether the event handler has completely handled the event or whether the system should continue its own processing.

(Inherited from HandledEventArgs)
RowIndex1

Gets the index of the row containing the first cell to compare.

RowIndex2

Gets the index of the row containing the second cell to compare.

SortResult

Gets or sets a value indicating the order in which the compared cells will be sorted.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

See also