DataGridView.ColumnHeaderMouseClick Händelse
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Inträffar när användaren klickar på en kolumnrubrik.
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
Händelsetyp
Exempel
När en kolumnrubrik klickas i är DataGridViewstandardbeteendet att sortera rutnätsraderna baserat på den klickade kolumnen eller att ändra sorteringsordningen om rutnätet redan sorteras efter den klickade kolumnen. I följande kodexempel visas hur du använder den här händelsen för att utföra en programmatisk sortering som emulerar standardbeteendet att klicka på en DataGridViewColumnHeaderCell när standardbeteendet har inaktiverats. I det här exemplet SelectionMode är inställt på ColumnHeaderSelect, så om DataGridViewColumnHeaderCell du klickar på väljer du innehållet i kolumnen. För att exempelkoden ska fungera som avsett måste standardbeteendet för kolumnrubrikklick ändras varje gång data läses in i DataGridView. Lägg till en DataBindingComplete händelsehanterare för att ange den kod som ändrar standardbeteendet. Om du vill köra det här exemplet klistrar du in koden i ett formulär som innehåller ett DataGridView namngivet dataGridView1 formulär och kontrollerar att alla händelser är associerade med deras händelsehanterare.
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
Kommentarer
Mer information om hur du hanterar händelser finns i Hantera och höja händelser.