Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
60
Sort rows, move rows with an empty value to the end
posted

 Situation:

    I have a grid with columns "NAME" and "DATE". When sorting (ascending) on date, all rows without a date should be placed at the end of the rowcollection (like when sorting descending).

    This is what i'm currently doing:

    Private Sub gridcandidates_AfterSortChange(ByVal sender As System.Object, ByVal e As Infragistics.Win.UltraWinGrid.BandEventArgs) Handles gridcandidates.AfterSortChange

        Dim col As UltraGridColumn = e.Band.Columns("DATE")
        If col.SortIndicator = SortIndicator.Ascending Then
            For i As Integer = gridcandidates.Rows.Count - 1 To 0 Step -1
            If Not IsDBNull(gridcandidates.Rows(i).Cells("DATE").Value) Then
                gridcandidates.Rows.Move(gridcandidates.Rows(i), 0) 'Move row to beginning
            Else
                gridcandidates.Rows.Move(gridcandidates.Rows(i), gridcandidates.Rows.Count - 1) 'Move row to end
            End If
        Next
        End If

    End Sub

 

Question/problems:

    - It messes up the sort... the columns aren't sorted by date.

    - Is there anyway to improve the code? I'm sure i'm overlooking something!

Parents
No Data
Reply
  • 469350
    Verified Answer
    Offline posted

    I think the actual sorting process may be asynchronous, so the rows might not actually be sorted by the time AfterSortChange fires. 

    You might be better off applying a SortComparer to the column. Your custom SortComparer class would have to know whether the column is being sorted Ascending or Descending (you could set a property on it from the BeforeSortChange event of the grid) and then sort the null values as higher or lower depending on the sort direction. 

Children