Translate

Samstag, 27. Februar 2016

VB.Net Datagridview Sortierung, aktuelle selektierte Zeilen und Scrollbarposition speichern

'Aktuelle Sortierung zwischenspeichern
Dim oldColumn As DataGridViewColumn = Datagridview.SortedColumn
Dim direction As ListSortDirection = Datagridview.SortOrder
Dim selectedItems As New List(Of Integer)
If Datagridview.SelectedRows.Count > 0 Then
    For Each r In Datagridview.SelectedRows
        selectedItems.Add(r.Index)
    Next
End If
'Aktuelle erste angezeigte Zeile speichern
Dim saveRow As Integer = 0
If Datagridview.Rows.Count > 0 Then
                If Not Datagridview.FirstDisplayedCell Is Nothing Then
                               saveRow = Datagridview.FirstDisplayedCell.RowIndex
                End If
End If

 'Your Code here

'Sortierung anwenden
If Not oldColumn Is Nothing Then
    Select Case direction
        Case 0
        Case 1
            Datagridview.Sort(oldColumn, ListSortDirection.Ascending) 'Datagridview sortieren
        Case 2
            Datagridview.Sort(oldColumn, ListSortDirection.Descending) 'Datagridview sortieren
    End Select
Else
    Datagridview.Sort(Datagridview.Columns(2), ListSortDirection.Descending) 'Datagridview sortieren
End If
'Alle SelectedRows resetten
For Each r In Datagridview.SelectedRows
    r.Selected = False
Next
'SelectedRows wieder setzen
If selectedItems.Count > 0 Then
    For Each r In selectedItems
        If Datagridview.Rows.Count > 0 Then
            If Datagridview.Rows.Count - 1 >= r Then
                Datagridview.Rows(r).Selected = True
            End If
        End If
    Next
End If
'Scrollbar wieder richtig setzen
If saveRow <> 0 And saveRow < Datagridview.Rows.Count Then
    Datagridview.FirstDisplayedScrollingRowIndex = saveRow
End If

Keine Kommentare:

Kommentar veröffentlichen