SelectionMode
property to FullColumnSelect
or ColumnHeaderSelect
.SortMode
property of the columns that can be sorted to Automatic
.ColumnHeaderMouseClick
event of the DataGridView.e.ColumnIndex
property of the DataGridViewCellMouseEventArgs
parameter.DataGridViewColumn
object for the clicked column header using the Columns[e.ColumnIndex]
property.Sort
method of the DataGridView with the DataGridViewColumn
and the sort direction as arguments.SortGlyphDirection
property of the clicked column header to display an arrow to indicate the sort direction.Example code:
private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
if (column.SortMode != DataGridViewColumnSortMode.Automatic)
{
return;
}
ListSortDirection direction = ListSortDirection.Ascending;
if (column.HeaderCell.SortGlyphDirection == SortOrder.Ascending)
{
direction = ListSortDirection.Descending;
}
dataGridView1.Sort(column, direction);
// Optionally, set the sort glyph direction
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
if (col != column)
{
col.HeaderCell.SortGlyphDirection = SortOrder.None;
}
}
column.HeaderCell.SortGlyphDirection = direction == ListSortDirection.Ascending ?
SortOrder.Ascending : SortOrder.Descending;
}
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-10-01 11:00:00 +0000
Seen: 12 times
Last updated: May 20 '22
How can I include the hours component to a DateTime column using PowerQuery?
Identify commonalities among the strings in a specific column of a DataFrame.
What is the procedure for using Pandas fillna() method with the column's mode?
How can you use linq to choose a specific column from a datatable?
What is the process of using a Word2Vec model on a column within a Pandas dataframe?