How do you sort on a datagridview? I can sort descending with this code, but want to click a second time and sort ascending. IOW, one click descending, the next ascending, and back and forth.
Code:
Private Sub DataGridView1_ColumnHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.ColumnHeaderMouseClick
If e.ColumnIndex >= 0 Then
Dim dataGridViewColumn As DataGridViewColumn = DataGridView1.Columns(e.ColumnIndex)
' Determine the sorting order based on the current sorting mode
Dim sortOrder As SortOrder = If(dataGridViewColumn.SortMode = DataGridViewColumnSortMode.Automatic, SortOrder.Ascending, SortOrder.Descending)
' Sort the DataGridView by the clicked column
DataGridView1.Sort(dataGridViewColumn, sortOrder)
End If
End Sub