DataGridViewSortCompareEventArgs 类

定义

SortCompare 事件提供数据。

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

示例

下面的代码示例演示如何 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

注解

可以处理 SortCompare 事件以提供自定义排序。 仅当未设置 属性且 属性设置为 falseDataGridView.DataSourceDataGridView.VirtualMode才会发生此事件。 对于要排序的列中的每对单元格,此事件发生一次。 仅当用户单击属性值AutomaticDataGridViewColumn.SortMode 的列的 标题时,或者调用DataGridView.Sort(DataGridViewColumn, ListSortDirection)重载时,才会发生此情况。 当属性值为 的ProgrammaticDataGridViewColumn.SortMode发生此事件时,必须通过 DataGridViewColumnHeaderCell.SortGlyphDirection 属性自行显示排序字形。

可以使用此事件通过一列或多列中的单元格值对行进行排序。 CellValue1使用 和 CellValue2 属性比较 属性中指定的列中的Column单元格值。 RowIndex1使用 和 RowIndex2 属性通过 DataGridView.Rows 集合访问其他列中的值。

构造函数

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

初始化 DataGridViewSortCompareEventArgs 类的新实例。

属性

CellValue1

获取要比较的第一个单元格的值。

CellValue2

获取要比较的第二个单元格的值。

Column

获取正在排序的列。

Handled

获取或设置一个值,该值指示事件处理程序是否已完整处理事件,或者系统是否应该继续本身的处理。

(继承自 HandledEventArgs)
RowIndex1

获取包含要比较的第一个单元格的行的索引。

RowIndex2

获取包含要比较的第二个单元格的行的索引。

SortResult

获取或设置一个值,该值指示被比较单元格将遵循的排序顺序。

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅