In my example, the column 'DEProduct' is sorted desc.
When I do a datagridview update, everything is restored directly to the default datasource values, and I lose my columnheader sorting.
I have a lot of columns, so how can I go back to this columnheader sorting, without checking them all before which one was selected, and trigger it manually after?
You mention datasource, so the only way I can see that you are seeing the described situation is that you are resetting the datasource such that the DataGridview rebinds to a new source.
You can store the current sort state using the DataGridView.SortedColumn Property and the DataGridView.SortOrder Property. Then do your update. After the update, restore the stored sort state.
' since it is asumed that the datasource changes, store only the column name
Dim sortColName As String = dgv1.SortedColumn?.Name 'dgv1.SortedColumn may be null
Dim direction As SortOrder = dgv1.SortOrder
UpdateData() ' replace this with your Update code
' restore the sort state if any
If sortColName IsNot Nothing Then
Dim columnToSort As DataGridViewColumn = dgv1.Columns(sortColName)
If columnToSort IsNot Nothing Then
Dim directionProgrammatic As System.ComponentModel.ListSortDirection
Select Case direction
Case SortOrder.Ascending
directionProgrammatic = System.ComponentModel.ListSortDirection.Ascending
Case SortOrder.Descending
directionProgrammatic = System.ComponentModel.ListSortDirection.Descending
End Select
dgv1.Sort(columnToSort, directionProgrammatic)
columnToSort.HeaderCell.SortGlyphDirection = direction
End If
End If