Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

DataGridViewSortCompareEventArgs Class

Provides data for the SortCompare event.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
public class DataGridViewSortCompareEventArgs : HandledEventArgs

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.

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;
}
System.Object
  System.EventArgs
    System.ComponentModel.HandledEventArgs
      System.Windows.Forms.DataGridViewSortCompareEventArgs
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

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
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.