共用方式為


DataGridView.ColumnHeaderMouseClick 事件

定義

使用者按一下資料行行首時發生。

public:
 event System::Windows::Forms::DataGridViewCellMouseEventHandler ^ ColumnHeaderMouseClick;
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 按一下資料行標題時,預設行為是根據按一下的資料行來排序方格資料列,或者,如果方格已經依按一下的資料行排序,則會反轉排序次序。 下列程式碼範例示範如何使用這個事件來執行程式設計排序,以模擬在停用預設行為時按一下 DataGridViewColumnHeaderCell 的預設行為。 在此範例中 SelectionMode , 會設定為 ColumnHeaderSelect ,因此按一下 會 DataGridViewColumnHeaderCell 選取資料行的內容。 若要讓範例程式碼如預期般運作,每次將資料載入 DataGridView 至 時,都必須變更預設資料行標頭按一下行為。 DataBindingComplete新增事件處理常式以提供變更預設行為的程式碼。 若要執行此範例,請將程式碼貼到包含 DataGridView 具名 dataGridView1 的表單中,並確定所有事件都與其事件處理常式相關聯。

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

備註

如需如何處理事件的詳細資訊,請參閱 處理和引發事件

適用於

另請參閱