I could not get the Stephen Lautier solution to work on VB.Net, but found another solution that might work.
Whenever a sort operation occurs, the following events occur in that order:
- Sorting
- UnloadingRow (for all rows in a DataGrid)
- Loading Row (for all rows in a DataGrid)
- Layoutupdated
This can be used as follows:
Variables
Private _updateSorted As Boolean Private _tempSender As Object Private _rowsLoaded As List(Of DataGridRowEventArgs) _rowsLoaded = New List(Of DataGridRowEventArgs)
Sorting
Private Sub myDataGrid_Sorting(sender As Object, e As DataGridSortingEventArgs) Handles myDataGrid.Sorting _updateSorted = True _rowsLoaded.Clear() _tempSender = Nothing End Sub
Upload / Download Event Strings
'Save pre-sorting state here, if desired' 'Perform operation on pre-sorting rows here, if desired' Private Sub myDataGrid_UnloadingRow(sender As Object, e As DataGridRowEventArgs) Handles myDataGrid.UnloadingRow End Sub 'Save post-sorting state here.' 'Perform operation on post-sorting rows here' 'In this example, the operation is dependent on the DataGrid updating its layout first so I included items relevant to handling that' Private Sub myDataGrid_LoadingRow(sender As Object, e As DataGridRowEventArgs) Handles myDataGrid.LoadingRow Dim myDataGridCell As DataGridCell = GetCellByRowColumnIndex(myDataGrid, e.Row.GetIndex, colIndex) 'Or whatever layout-dependent object you are using, perhaps utilizing e As DataGridRowEventArgs' If Not IsNothing(myDataGridCell) Then '~~ Perform operations here ~~' Else If _updateSorted Then 'Update has occurred but the updated DataGrid is not yet available' 'Save variables to use once the DataGrid is updated' _rowsLoaded.Add(e) _tempSender = sender End If End If End Sub
Layoutupdated
Private Sub myDataGrid_LayoutUpdated(sender As Object, e As EventArgs) Handles myDataGrid.LayoutUpdated If _updateSorted Then Dim rowsLoaded As New List(Of DataGridRowEventArgs) For Each eRow As DataGridRowEventArgs In _rowsLoaded rowsLoaded.Add(eRow) Next For Each eRow As DataGridRowEventArgs In rowsLoaded 'Now perform the action to the sorted DataGridRows in the order they were added' myDataGrid_LoadingRow(_tempSender, eRow) Next _updateSorted = False End If End Sub
PellucidWombat
source share