DataGridViewSortCompareEventHandler 委托

定义

表示将处理 SortCompare 控件的 DataGridView 事件的方法。

public delegate void DataGridViewSortCompareEventHandler(System::Object ^ sender, DataGridViewSortCompareEventArgs ^ e);
public delegate void DataGridViewSortCompareEventHandler(object sender, DataGridViewSortCompareEventArgs e);
public delegate void DataGridViewSortCompareEventHandler(object? sender, DataGridViewSortCompareEventArgs e);
type DataGridViewSortCompareEventHandler = delegate of obj * DataGridViewSortCompareEventArgs -> unit
Public Delegate Sub DataGridViewSortCompareEventHandler(sender As Object, e As DataGridViewSortCompareEventArgs)

参数

sender
Object

事件源。

示例

下面的代码示例演示如何 SortCompare 在多列排序中使用 。 此示例是 How to: Customize Sorting in the Windows 窗体 DataGridView 控件中提供的更大示例的一部分。

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

注解

创建 DataGridViewSortCompareEventHandler 委托时,需要标识将处理该事件的方法。 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。 有关事件处理程序委托的详细信息,请参阅 处理和引发事件

扩展方法

GetMethodInfo(Delegate)

获取指示指定委托表示的方法的对象。

适用于

另请参阅