다음을 통해 공유


DataGridView.SortCompare 이벤트

정의

정렬 작업을 수행하기 위해 두 셀 값을 비교할 때 DataGridView 발생합니다.

public:
 event System::Windows::Forms::DataGridViewSortCompareEventHandler ^ SortCompare;
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 

이벤트 유형

예제

다음 코드 예제에서는 여러 열 정렬에서 사용 SortCompare 하는 방법을 보여 줍니다. 이 예제는 방법: Windows Forms 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

설명

이 이벤트는 속성이 DataSource 설정되지 않고 속성 값falseVirtualMode 될 때만 발생합니다.

이 이벤트는 정렬되는 열의 셀 쌍을 비교합니다. 사용자가 속성 값Automatic이 있는 열의 머리글을 클릭하거나 오버로드를 SortMode 호출할 Sort(DataGridViewColumn, ListSortDirection) 때만 발생합니다. 속성 값Programmatic이 있는 열 SortMode 에 대해 이 이벤트가 발생하면 속성을 통해 DataGridViewColumnHeaderCell.SortGlyphDirection 정렬 문자 모양을 직접 표시해야 합니다.

이 이벤트를 사용하여 한 열 또는 여러 열의 셀 값을 사용하여 행을 정렬할 수 있습니다. 속성 및 CellValue1CellValue2 속성을 사용하여 속성에 지정된 열의 셀 값을 비교합니다 Column . RowIndex1 RowIndex2 및 속성을 사용하여 컬렉션을 통해 다른 열의 값에 Rows 액세스합니다.

이벤트를 처리하는 방법에 대한 자세한 내용은 이벤트 처리 및 발생을 참조하세요.

적용 대상

추가 정보