DataGridViewSortCompareEventArgs DataGridViewSortCompareEventArgs DataGridViewSortCompareEventArgs DataGridViewSortCompareEventArgs Class

定义

SortCompare 事件提供数据。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
继承
DataGridViewSortCompareEventArgsDataGridViewSortCompareEventArgsDataGridViewSortCompareEventArgsDataGridViewSortCompareEventArgs

示例

下面的代码示例演示如何SortCompare在多列排序中使用。The following code example demonstrates the use of SortCompare in a multiple column sort. 此示例是在如何:自定义 Windows 窗体 DataGridView 控件中的排序。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

注解

可以处理SortCompare事件以提供自定义排序。You can handle the SortCompare event to provide custom sorting. 仅当未设置DataGridView.DataSource属性DataGridView.VirtualMode并且属性设置为false时, 才会发生此事件。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. 仅当用户单击DataGridViewColumn.SortMode属性值为的Automatic列的标头时, DataGridView.Sort(DataGridViewColumn, ListSortDirection)或在调用重载时才会发生这种情况。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. 对于DataGridViewColumn.SortMode属性值为的Programmatic列发生此事件时, 必须通过DataGridViewColumnHeaderCell.SortGlyphDirection属性自行显示排序标志符号。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. CellValue1使用和CellValue2属性来比较Column属性中指定的列中的单元值。Use the CellValue1 and CellValue2 properties to compare cell values in the column specified in the Column property. RowIndex1使用和RowIndex2属性可以通过DataGridView.Rows集合访问其他列中的值。Use the RowIndex1 and RowIndex2 properties to access values in other columns through the DataGridView.Rows collection.

构造函数

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

初始化 DataGridViewSortCompareEventArgs 类的新实例。Initializes a new instance of the DataGridViewSortCompareEventArgs class.

属性

CellValue1 CellValue1 CellValue1 CellValue1

获取要比较的第一个单元格的值。Gets the value of the first cell to compare.

CellValue2 CellValue2 CellValue2 CellValue2

获取要比较的第二个单元格的值。Gets the value of the second cell to compare.

Column Column Column Column

获取正在排序的列。Gets the column being sorted.

Handled Handled Handled 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 RowIndex1 RowIndex1 RowIndex1

获取包含要比较的第一个单元格的行的索引。Gets the index of the row containing the first cell to compare.

RowIndex2 RowIndex2 RowIndex2 RowIndex2

获取包含要比较的第二个单元格的行的索引。Gets the index of the row containing the second cell to compare.

SortResult SortResult SortResult SortResult

获取或设置一个值,该值指示被比较单元格将遵循的排序顺序。Gets or sets a value indicating the order in which the compared cells will be sorted.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

适用于

另请参阅