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 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 事件以提供自訂排序。 只有在屬性未設定且 DataGridView.VirtualMode 屬性設定為 false 時, DataGridView.DataSource 才會發生此事件。 針對要排序的資料行中每一對儲存格,就會發生此事件一次。 只有當使用者按一下屬性值 Automatic 為 的資料行 DataGridViewColumn.SortMode 標頭,或呼叫 DataGridView.Sort(DataGridViewColumn, ListSortDirection) 多載時,才會發生此情況。 當具有 屬性值 Programmatic 的資料行發生 DataGridViewColumn.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)

適用於

另請參閱