简体   繁体   中英

Dynamic Multiple Column Sort on a DataGridView

We have a custom implementation of the DataGridView (it does not do much, just handles formatting and the like). All DataGridView controls are bound to a list of custom objects ( System.Generic.List(Of MyObjectName) ). I have been tasked with making all of these grids sortable on multiple columns (I will start with two, and alter again later if needed).

Currently, sorting of these grids is handled by calling a method to convert the List(Of T) to an IBindingList. The sort is then done:

Private Sub DoSort()
    sortedList.Clear()
    If sortBy Is Nothing Then
        For Each obj As Object In BaseList
            sortedList.Add(New ListItem(obj, obj))
        Next
    Else
        If (sortBy.PropertyType Is GetType(Date)) Then
            For Each obj As Object In BaseList
                sortedList.Add(New ListItem(DirectCast(sortBy.GetValue(obj), Date), obj))
            Next
        ElseIf (sortBy.PropertyType Is GetType(Integer)) Then
            For Each obj As Object In BaseList
                sortedList.Add(New ListItem(DirectCast(sortBy.GetValue(obj), Integer), obj))
            Next
        Else
            For Each obj As Object In BaseList
                sortedList.Add(New ListItem(sortBy.GetValue(obj), obj))
            Next
        End If
    End If
    sortedList.Sort()
    m_isSorted = True
    RaiseEvent ListChanged(Me, New ListChangedEventArgs(ListChangedType.Reset, 0))
End Sub

I tried and failed to add another column to this. When asked to add multiple column sorting to a particular grid I cheated and sorted that list directly. I then tried to use that same code to modify our custom grid control:

Protected Overrides Sub OnColumnHeaderMouseClick(e As DataGridViewCellMouseEventArgs)
    MyBase.OnColumnHeaderMouseClick(e)

    If TypeOf (Me.DataSource) Is IList AndAlso Me.DataSource.GetType().IsGenericType Then
        If _strSortcolumn02 = "" Then
            _strSortColumn01 = Me.Columns(e.ColumnIndex).Name
            _strSortcolumn02 = Me.Columns(e.ColumnIndex).Name
        Else
            _strSortColumn01 = _strSortcolumn02
            _strSortcolumn02 = Me.Columns(e.ColumnIndex).Name
        End If

        If Me.Columns(e.ColumnIndex).HeaderCell.SortGlyphDirection = SortOrder.Ascending Then
            _soSortDirection = Windows.Forms.SortOrder.Descending
        Else
            _soSortDirection = Windows.Forms.SortOrder.Ascending
        End If

        'Dim tType As Type = Me.DataSource.GetType()
        'Dim typeName As String = String.Format("System.Collections.Generic.List`1[[{0}]], mscorlib", tType.AssemblyQualifiedName)
        'Dim lst As New List(Of Type.GetType(typeName))
        Dim lst As Object = Me.DataSource

        If _soSortDirection = Windows.Forms.SortOrder.Ascending Then
            Me.DataSource = lst.OrderBy(Function(x) x.GetType().GetProperty(_strSortColumn01).GetValue(x)). _
                                ThenBy(Function(x) x.GetType().GetProperty(_strSortcolumn02).GetValue(x)).ToList()
        Else
            Me.DataSource = lst.OrderByDescending(Function(x) x.GetType().GetProperty(_strSortColumn01).GetValue(x)). _
                                          ThenByDescending(Function(x) x.GetType().GetProperty(_strSortcolumn02).GetValue(x)).ToList()
        End If
    End If
End Sub

The last If Block is the code that works when given a statically typed List(Of T). However, when trying to dynamically determine the type, this code fails. It does so in a rather frustrating manner, given that at runtime the lst variable is the correct type, but I get an error on the OrderBy:

A first chance exception of type 'System.MissingMemberException' occurred in Microsoft.VisualBasic.dll

Additional information: Public member 'OrderBy' on type 'List(Of CustomerInquiryGridBE)' not found.

It is important to note that List(Of CustomerInquiryGridBE) is the actual type of the DataSource, and this exact same code works just fine when the type is explicitly provided. Is there anyway to make this work?

First I must say, I found a way to do with Dataset>DataTable for application with direct connection to Database.
As we already know from MSDN:

 *To support sorting, the underlying list must implement the IBindingList or IBindingListView, interfaces. This capability can be queried through the SupportsSorting property. Multicolumn sorting is available when the SupportsAdvancedSorting property is true.
Setting the Sort property will change the internal list depending on its type:
If the list is of type IBindingList, the IBindingList.SortProperty and IBindingList.SortDirection properties are set in the internal list.
If the list is of type IBindingListView, the IBindingListView.SortDescriptions property is set.*

But I have a Winform app using MVP pattern with EF6
In my presenter I already had List(Of T) , and I don't want to change a working code.
This Class I converted by hand from an example in C# from MSDN, in my case works!
First inport this classs in your project: SortableBindingList

 Imports System.ComponentModel
 Imports System.Reflection

 Namespace Repository

Public Class SortableBindingList(Of T)
    Inherits BindingList(Of T)
    Private sortedList As ArrayList
    Private unsortedItems As ArrayList
    Private isSortedValue As Boolean

    Public Sub New()
    End Sub

    Public Sub New(list As IList(Of T))
        For Each o As Object In list
            Me.Add(DirectCast(o, T))
        Next
    End Sub

    Protected Overrides ReadOnly Property SupportsSearchingCore() As Boolean
        Get
            Return True
        End Get
    End Property

    Protected Overrides Function FindCore(prop As PropertyDescriptor, key As Object) As Integer
        Dim propInfo As PropertyInfo = GetType(T).GetProperty(prop.Name)
        Dim item As T

        If Not (key Is Nothing) Then
            Dim i As Integer = 0
            While i < Count
                item = DirectCast(Items(i), T)
                If propInfo.GetValue(item, Nothing).Equals(key) Then
                    Return i
                End If
                System.Threading.Interlocked.Increment(i)
            End While
        End If
        Return -1
    End Function

    Public Function Find([property] As String, key As Object) As Integer
        Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(GetType(T))
        Dim prop As PropertyDescriptor = properties.Find([property], True)

        If IsNothing(prop) Then
            Return -1
        Else
            Return FindCore(prop, key)
        End If
    End Function

    Protected Overrides ReadOnly Property SupportsSortingCore() As Boolean
        Get
            Return True
        End Get
    End Property


    Protected Overrides ReadOnly Property IsSortedCore() As Boolean
        Get
            Return isSortedValue
        End Get
    End Property

    Private sortDirectionValue As ListSortDirection
    Private sortPropertyValue As PropertyDescriptor

    Protected Overrides Sub ApplySortCore(prop As PropertyDescriptor, direction As ListSortDirection)
        sortedList = New ArrayList()

        '// Check to see if the property type we are sorting by implements
        '// the IComparable interface.
        Dim interfaceType As Type = prop.PropertyType.GetInterface("IComparable")

        If interfaceType = Nothing AndAlso prop.PropertyType.IsValueType Then
            Dim underlyingType As Type = Nullable.GetUnderlyingType(prop.PropertyType)

            If Not (underlyingType Is Nothing) Then
                interfaceType = underlyingType.GetInterface("IComparable")
            End If
        End If

        If Not (interfaceType Is Nothing) Then
            '// If so, set the SortPropertyValue and SortDirectionValue.
            sortPropertyValue = prop
            sortDirectionValue = direction

            Dim query As IEnumerable(Of T) = MyBase.Items
            If direction = ListSortDirection.Ascending Then
                query = query.OrderBy(Function(i) prop.GetValue(i))
            Else
                query = query.OrderByDescending(Function(i) prop.GetValue(i))
            End If
            Dim newIndex As Integer = 0
            For Each item As Object In query
                Me.Items(newIndex) = DirectCast(item, T)
                System.Math.Max(System.Threading.Interlocked.Increment(newIndex), newIndex - 1)
            Next
            isSortedValue = True

            Me.OnListChanged(New ListChangedEventArgs(ListChangedType.Reset, -1))
        Else
            Throw New NotSupportedException("Cannot sort by " + prop.Name + ". This" + prop.PropertyType.ToString() + " does not implement IComparable")
        End If
    End Sub

    Protected Overrides Sub RemoveSortCore()
        Dim position As Integer
        Dim temp As Object

        If Not (unsortedItems Is Nothing) Then
            Dim i As Integer = 0
            While i < unsortedItems.Count
                position = Me.Find("LastName", unsortedItems(i).[GetType]().GetProperty("LastName").GetValue(unsortedItems(i), Nothing))
                If position > 0 AndAlso position <> i Then
                    temp = Me(i)
                    Me(i) = Me(position)
                    Me(position) = DirectCast(temp, T)
                    System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
                ElseIf position = i Then
                    System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
                Else
                    unsortedItems.RemoveAt(i)
                End If
            End While
            isSortedValue = False
            OnListChanged(New ListChangedEventArgs(ListChangedType.Reset, -1))
        End If
    End Sub

    Public Sub RemoveSort()
        RemoveSortCore()
    End Sub
    Protected Overrides ReadOnly Property SortPropertyCore() As PropertyDescriptor
        Get
            Return sortPropertyValue
        End Get
    End Property

    Protected Overrides ReadOnly Property SortDirectionCore() As ListSortDirection
        Get
            Return sortDirectionValue
        End Get
    End Property

    Public Sub myPublicSort(ByRef prop As PropertyDescriptor, direction As ListSortDirection)
        ApplySortCore(prop, direction)
    End Sub

 End Class
 End Namespace

Change Namspace to yours !

Then in my Form Load()
Here I converted my initial List(Of T) to sortableBindingList(Of T)
Assign this SortableBindingList as DataSource of BindingSource, And don't forget to set DataGridView DataSource to YourBindingSource (in my case PatientBindingSource)

 For Each item As Patient In _presenter.GetAllPatient()  ' my List(Of Patients)
        sortableBList.Add(item)
 Next

    Try
        PatientBindingSource.DataSource = sortableBList                              '_presenter.listaPacienti
    Catch ex As ArgumentException
        Console.WriteLine(ex)
    End Try
    PatientBindingSource.ResetBindings(False)

After that Add this

  'this is for Programatic Sort
  Private Sub dgvPacient_DataBindingComplete(sender As Object, e As DataGridViewBindingCompleteEventArgs) Handles dgvPacient.DataBindingComplete
    For Each Column As DataGridViewColumn In dgvPacient.Columns
        Column.SortMode = DataGridViewColumnSortMode.Programmatic
    Next Column
End Sub


'click on Column Header to sort
Private Sub dgvPacient_ColumnHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvPacient.ColumnHeaderMouseClick
    If isInitDone Then
        Try
            Debug.WriteLine("click pe header !")

            Dim ColumnNameBruteFromDGV As String = dgvPacient.Columns(e.ColumnIndex).Name
            Dim ColumnName As String = ColumnNameBruteFromDGV.Replace("DataGridViewTextBoxColumn", "")
            Dim lastColumnDir As String = ""
            Dim SortDirection As ListSortDirection

            If IsNothing(PatientBindingSource.Sort) Then
                SortDirection = ListSortDirection.Ascending
                PatientBindingSource.Sort = "LastName" & " ASC"
            Else
                If PatientBindingSource.Sort.Contains(lastColumnName) Then
                    If PatientBindingSource.Sort.Contains("DESC") Then
                        PatientBindingSource.Sort = lastColumnName & lastColumnDir & " " & ColumnName & " ASC"
                        SortDirection = ListSortDirection.Ascending
                    Else
                        PatientBindingSource.Sort = lastColumnName & lastColumnDir & " " & ColumnName & " DESC"
                        SortDirection = ListSortDirection.Descending
                    End If
                Else
                    If PatientBindingSource.Sort.Contains("DESC") Then
                        lastColumnName = ColumnName
                        lastColumnDir = " DESC"
                        PatientBindingSource.Sort = lastColumnName & lastColumnDir
                        SortDirection = ListSortDirection.Descending
                    Else
                        lastColumnName = ColumnName
                        lastColumnDir = " ASC"
                        PatientBindingSource.Sort = lastColumnName & lastColumnDir
                        SortDirection = ListSortDirection.Ascending
                    End If
                End If

            End If

            ' ColumnName

            dgvPacient.Columns(e.ColumnIndex).HeaderCell.SortGlyphDirection = _
                If(SortDirection = ListSortDirection.Ascending, _
                   SortOrder.Ascending, _
                   SortOrder.Descending _
            )

        Catch ex As ArgumentException
            Debug.WriteLine("dgvPacient_ColumnHeaderMouseClick Exception : " & ex.Message)
        End Try
    End If
End Sub

change or replace PatientBindingSource with your BindingSourceName
and replace dgvPacient with yourDataGridViewName ..
I have a little issuse that I have no time now, with HeaderCell.SortGlyphDirection, If I click on a colun header, is sorted, and click on other column , and is sorted by 2 columns, but now I have GlyphDirection just on last column..
Happy Codding :)
CristiC777

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM