locked
Sorting Datagrid by code RRS feed

  • Question

  • I'm trying to implement multiple sorts on a datagrid using this example groupsort but when I copy the code into my screens vb file I get an errors saying

    'FindControl' is not declared. It may be inaccessible due to its protection level.

     Public Class GroupedGrid
    
            Private _grid As IContentItemProxy
    
            Private Sub GroupedGrid_Created()
                _grid = FindControl("grid")
                AddHandler _grid.ControlAvailable, AddressOf GRID_AVAILABLE
            End Sub
    
            Private Sub GRID_AVAILABLE(sender As Object, e As ControlAvailableEventArgs)
    
                Dim view As PagedCollectionView = New PagedCollectionView(CType(e.Control, DataGrid).ItemsSource)
    
                Using view.DeferRefresh()
                    view.GroupDescriptions.Clear()
    
                    ' What do you want to order by
                    view.SortDescriptions.Add(New SortDescription("LastName", ListSortDirection.Ascending))
    
                    ' What you want to group by
                    view.GroupDescriptions.Add(New PropertyGroupDescription("Gender"))
                End Using
    
                CType(e.Control, DataGrid).ItemsSource = view
            End Sub
    
        End Class

    I've got the Imports System.Windows.Data and Imports System.Windows.Controls at the top of my code.

    Not sure exaclty what "First, add Client project reference to System.Windowns.Controls.Data." in the example means, but I have got system.windows.controls.data in the .net tab under the menu Projects-Add References.

    I dont need the grouping, just want to have command buttons so I can sort by surname or forename.

    Thanks Grant



    Tuesday, August 14, 2012 5:14 AM

Answers

  • Wow, my head is fizzing now! So, after reading the other post it seems that I was right about disabling the sort for the user and if you want to have some sort of sorting you do it in a controlled way by manipulating the PageCollectionView of the grouped grid. If you go back to my original posting you could follow the Sliverlight code to add a select list of sort options.

    As a side note, I hoped that you marked my original post as helpful :)


    If you found this post helpful, please mark it as helpful. If by some chance I answered the question, please mark the question as answered. That way you will help more people like me :)

    • Marked as answer by NZTechworks Wednesday, August 15, 2012 11:52 PM
    Wednesday, August 15, 2012 5:34 AM

All replies

  • Hi Grant

    I'm glad that my post is proving to be helpful. I am slightly confused about your question though. The standard Datagrid allows you to sort by just clicking the column headings (unless you disable the option of course) so are you not wanting the groupings and you just want a button or buttons for the user to click in order to do the sorting?

    Let me know and I will see if I can knock up some code for you :)


    If you found this post helpful, please mark it as helpful. If by some chance I answered the question, please mark the question as answered. That way you will help more people like me :)

    Tuesday, August 14, 2012 7:47 AM
  • yes, my collections default sort is Surname/forename, but if a user sorts the surname column manually, it does not apply the second forename sort, so they cant return to the original sort order, or can I catch the users column heading click and overide it with a custom sort?

    I didnt realise you could do grouping in a datagrid either, so I also want to try using a grouped datagrit for outputting to xls as a workaround to the lack of a report writer.

    Cheers

    Grant

    Tuesday, August 14, 2012 9:15 PM
  • Now I am more confused. Firstly do you want the user to play around with the sort? You can disable the user changing the sort order so it will stay how you want it. Secondly are you able to group your datagrid? Lastly, how is your last comment the answer to your question when it is still full of questions???

    If you found this post helpful, please mark it as helpful. If by some chance I answered the question, please mark the question as answered. That way you will help more people like me :)

    Wednesday, August 15, 2012 5:06 AM
  • Ha.... your not the only one!

    See my other post re sorting , it might clear things up a little.

    Yes I have got my datagrid to group see blow:

    This grouping not really necessary, but I'm sure I will find a use for it somewhere!

    I've got side tracked with the grouping, and havnt yet got my multiple column sort working, but I'll close off this post as answered and take another look at the sorting.

    Cheers

    Grant

    Wednesday, August 15, 2012 5:26 AM
  • Wow, my head is fizzing now! So, after reading the other post it seems that I was right about disabling the sort for the user and if you want to have some sort of sorting you do it in a controlled way by manipulating the PageCollectionView of the grouped grid. If you go back to my original posting you could follow the Sliverlight code to add a select list of sort options.

    As a side note, I hoped that you marked my original post as helpful :)


    If you found this post helpful, please mark it as helpful. If by some chance I answered the question, please mark the question as answered. That way you will help more people like me :)

    • Marked as answer by NZTechworks Wednesday, August 15, 2012 11:52 PM
    Wednesday, August 15, 2012 5:34 AM