Grid refuses to sort

Dracarnion

Member
Joined
Apr 10, 2007
Messages
11
Programming Experience
5-10
I'm using a DataGridView in a .NET application and I'm trying to allow the user to sort the rows when clicking the column headers.

By default, these should be sorting automatically when clicked, but are not behaving as such. The sortmode property on these columns was set to automatic.

After accepting that the grid will not sort automatically, I have the sortmode property set to "programatic" and have been trying every method i can think of to make this data sort. I would just sort and reload all the data, but the data isn't populated all at once and can be added to the grid in increments.

Here's the code I've been trying:

VB.NET:
Private Sub gridReceived_ColumnHeaderMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles gridReceived.ColumnHeaderMouseClick

        Select Case e.ColumnIndex
            Case Me.gridReceived.Columns("ItemName").Index
                CType(Me.gridReceived.DataSource, SuperCollection(Of InvoiceLineItemAccepter)).ApplySort _
                (TypeDescriptor.GetProperties(GetType(InvoiceLineItemAccepter)).Find("ItemName", True), ListSortDirection.Ascending)
            Case Me.gridReceived.Columns("ItemID").Index
                CType(Me.gridReceived.DataSource, SuperCollection(Of InvoiceLineItemAccepter)).ApplySort _
                (TypeDescriptor.GetProperties(GetType(InvoiceLineItemAccepter)).Find("ItemID", True), ListSortDirection.Ascending)
            Case Me.gridReceived.Columns("PurchaseOrderID").Index
                CType(Me.gridReceived.DataSource, SuperCollection(Of InvoiceLineItemAccepter)).ApplySort _
                (TypeDescriptor.GetProperties(GetType(InvoiceLineItemAccepter)).Find("PurchaseOrderID", True), ListSortDirection.Ascending)
        End Select

        Me.InvoiceLineItemAccepterBindingSource.DataSource = Me.gridReceived.DataSource
        Me.gridReceived.DataSource = Me.InvoiceLineItemAccepterBindingSource.DataSource
    End Sub

I've tried many variations of this code, including attempts to manipulate the attached binding source, manipulating the grid itself, storing the data, sorting it, then reloading it, but nothing seems to make this data budge.

Any help with this is greatly appreciate and thanks for reading!
 
Back
Top