DataGridView.SortCompare 事件

定义

DataGridView 对两个单元格的值进行比较以执行排序操作时发生。Occurs when the DataGridView compares two cell values to perform a sort operation.

public:
 event System::Windows::Forms::DataGridViewSortCompareEventHandler ^ SortCompare;
public event System.Windows.Forms.DataGridViewSortCompareEventHandler SortCompare;
member this.SortCompare : System.Windows.Forms.DataGridViewSortCompareEventHandler 
Public Custom Event SortCompare As DataGridViewSortCompareEventHandler 

示例

下面的代码示例演示如何在多列排序中使用 SortCompareThe following code example demonstrates how to use the SortCompare in a multiple column sort. 此示例摘自 [How 中提供的一个更大的示例:自定义 Windows 窗体 DataGridView 控件 @ no__t 中的排序。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

注解

仅当未设置 DataSource 属性并且 @no__t 属性值 @no__t 为-2 时,才发生此事件。This event occurs only when the DataSource property is not set and the VirtualMode property value is false.

此事件比较要排序的列中的单元对。This event compares pairs of cells in the column being sorted. 仅当用户单击 SortMode 属性值为 Automatic 的列的标题或调用 Sort(DataGridViewColumn, ListSortDirection) 重载时,才发生此事件。It occurs only when the user clicks the header of a column with a SortMode property value of Automatic, or when you call the Sort(DataGridViewColumn, ListSortDirection) overload. 如果对值为 @no__t @no__t 的列执行此事件,则必须通过 @no__t 2 属性自行显示排序标志符号。When this event occurs for a column with a 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. 使用 CellValue1CellValue2 属性来比较 Column 属性中指定的列中的单元值。Use the CellValue1 and CellValue2 properties to compare cell values in the column specified in the Column property. 使用 @no__t 0 和 RowIndex2 属性通过 @no__t 集合访问其他列中的值。Use the RowIndex1 and RowIndex2 properties to access values in other columns through the Rows collection.

有关如何处理事件的详细信息,请参阅处理和引发事件For more information about how to handle events, see Handling and Raising Events.

适用于

另请参阅