DataGridView.ColumnHeaderMouseClick 이벤트

정의

열 머리글을 클릭할 때 발생합니다.Occurs when the user clicks a column header.

public:
 event System::Windows::Forms::DataGridViewCellMouseEventHandler ^ ColumnHeaderMouseClick;
public event System.Windows.Forms.DataGridViewCellMouseEventHandler ColumnHeaderMouseClick;
member this.ColumnHeaderMouseClick : System.Windows.Forms.DataGridViewCellMouseEventHandler 
Public Custom Event ColumnHeaderMouseClick As DataGridViewCellMouseEventHandler 

예제

DataGridView에서 열 머리글을 클릭 하면 기본 동작은 클릭 된 열을 기준으로 표 행의 순서를 정렬 하거나, 표가 이미 클릭 된 열을 기준으로 정렬 된 경우 정렬 순서를 반대로 하는 것입니다.When a column heading is clicked in a DataGridView, the default behavior is to order the grid rows based on the clicked column, or to reverse the sort order if the grid is already sorted by the clicked column. 다음 코드 예제에서는이 이벤트를 사용 하 여 기본 동작을 사용 하지 않도록 설정 된 경우 DataGridViewColumnHeaderCell를 클릭 하는 기본 동작을 에뮬레이트하는 프로그래밍 방식 정렬을 수행 하는 방법을 보여 줍니다.The following code example demonstrates how to use this event to perform a programmatic sort that emulates the default behavior of clicking a DataGridViewColumnHeaderCell when the default behavior has been disabled. 이 예에서는 SelectionMode ColumnHeaderSelect로 설정 되어 있으므로 DataGridViewColumnHeaderCell를 클릭 하면 열의 내용이 선택 됩니다.In this example, the SelectionMode is set to ColumnHeaderSelect, so clicking the DataGridViewColumnHeaderCell selects the contents of the column. 예제 코드가 의도 한 대로 작동 하려면 데이터가 DataGridView로드 될 때마다 기본 열 머리글 클릭 동작을 변경 해야 합니다.For the example code to function as intended, the default column header click behavior needs to be changed each time the data is loaded into the DataGridView. DataBindingComplete 이벤트 처리기를 추가 하 여 기본 동작을 변경 하는 코드를 제공 합니다.Add a DataBindingComplete event handler to provide the code that changes the default behavior. 이 예제를 실행 하려면 dataGridView1 라는 DataGridView 포함 된 양식에 코드를 붙여넣고 모든 이벤트가 해당 이벤트 처리기와 연결 되어 있는지 확인 합니다.To run this example, paste the code into a form that contains a DataGridView named dataGridView1 and ensure that all events are associated with their event handlers.

private void dataGridView1_ColumnHeaderMouseClick(
    object sender, DataGridViewCellMouseEventArgs e)
{
    DataGridViewColumn newColumn = dataGridView1.Columns[e.ColumnIndex];
    DataGridViewColumn oldColumn = dataGridView1.SortedColumn;
    ListSortDirection direction;

    // If oldColumn is null, then the DataGridView is not sorted.
    if (oldColumn != null)
    {
        // Sort the same column again, reversing the SortOrder.
        if (oldColumn == newColumn &&
            dataGridView1.SortOrder == SortOrder.Ascending)
        {
            direction = ListSortDirection.Descending;
        }
        else
        {
            // Sort a new column and remove the old SortGlyph.
            direction = ListSortDirection.Ascending;
            oldColumn.HeaderCell.SortGlyphDirection = SortOrder.None;
        }
    }
    else
    {
        direction = ListSortDirection.Ascending;
    }

    // Sort the selected column.
    dataGridView1.Sort(newColumn, direction);
    newColumn.HeaderCell.SortGlyphDirection =
        direction == ListSortDirection.Ascending ?
        SortOrder.Ascending : SortOrder.Descending;
}

private void dataGridView1_DataBindingComplete(object sender,
    DataGridViewBindingCompleteEventArgs e)
{
    // Put each of the columns into programmatic sort mode.
    foreach (DataGridViewColumn column in dataGridView1.Columns)
    {
        column.SortMode = DataGridViewColumnSortMode.Programmatic;
    }
}
Private Sub dataGridView1_ColumnHeaderMouseClick(ByVal sender As Object, _
    ByVal e As DataGridViewCellMouseEventArgs) _
    Handles dataGridView1.ColumnHeaderMouseClick

    Dim newColumn As DataGridViewColumn = _
        dataGridView1.Columns(e.ColumnIndex)
    Dim oldColumn As DataGridViewColumn = dataGridView1.SortedColumn
    Dim direction As ListSortDirection

    ' If oldColumn is null, then the DataGridView is not currently sorted.
    If oldColumn IsNot Nothing Then

        ' Sort the same column again, reversing the SortOrder.
        If oldColumn Is newColumn AndAlso dataGridView1.SortOrder = _
            SortOrder.Ascending Then
            direction = ListSortDirection.Descending
        Else

            ' Sort a new column and remove the old SortGlyph.
            direction = ListSortDirection.Ascending
            oldColumn.HeaderCell.SortGlyphDirection = SortOrder.None
        End If
    Else
        direction = ListSortDirection.Ascending
    End If

    ' Sort the selected column.
    dataGridView1.Sort(newColumn, direction)
    If direction = ListSortDirection.Ascending Then
        newColumn.HeaderCell.SortGlyphDirection = SortOrder.Ascending
    Else
        newColumn.HeaderCell.SortGlyphDirection = SortOrder.Descending
    End If

End Sub

Private Sub dataGridView1_DataBindingComplete(ByVal sender As Object, _
    ByVal e As DataGridViewBindingCompleteEventArgs) _
    Handles dataGridView1.DataBindingComplete

    ' Put each of the columns into programmatic sort mode.
    For Each column As DataGridViewColumn In dataGridView1.Columns
        column.SortMode = DataGridViewColumnSortMode.Programmatic
    Next
End Sub

설명

이벤트를 처리 하는 방법에 대 한 자세한 내용은 참조 하세요. 이벤트 처리 및 발생합니다.For more information about how to handle events, see Handling and Raising Events.

적용 대상

추가 정보