Had been wrecking my head with this for a few hours but got a good solution (with very little code).
Okay so it using reflection and mightn't have order log n speed, but so what if you are sorting collections with normally less than 50 items.
I handle the Column Header Mouse Click
Get the data property name.
Then use some reflection to sort the generic list.  (before this I had to use select case, and sort by knowing the property name in code, but when I add new properties this was a pain to maintain).
    Private Sub SelectionsCollectionsDataGridView_ColumnHeaderMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles SelectionsCollectionsDataGridView.ColumnHeaderMouseClick
        Try
            Dim col = Me.SelectionsCollectionsDataGridView.Columns(e.ColumnIndex)
            Me.selectedMarket.SelectionsCollections.Sort(Function(p1, p2) p1.GetType.GetProperty(col.DataPropertyName).GetValue(p1, Nothing).CompareTo(p2.GetType.GetProperty(col.DataPropertyName).GetValue(p2, Nothing)))
            Me.SelectionsCollectionsBindingSource.CurrencyManager.Refresh()
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical)
        End Try
    End Sub
 
No comments:
Post a Comment