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

注解

有关如何处理事件的详细信息,请参阅 处理和引发事件

适用于

另请参阅