I have a GridView set up in the following way:
- bound to a
List<T>
in code-behind (I am using my own custom BOL) - no DataSource Object on the HTML page
- sortable on each column that I choose (the
SortExpression
s are all set correctly)
However, I am getting the following error message:
The GridView 'myGridView' fired event Sorting which wasn't handled.
What is the best way for me to get my List<T>
to allow sorting?
I am suspecting that it will have to do with specifying a function for the OnSorting
attribute, i.e.:
OnSorting = "MySortingMethod"
If you get this error:
Try adding
.ToList<T>()
to your query:Thank you for your answers on the sorting. I turned to LINQ to help sort dynamically. Since the grid knows whether to sort ASC or DESC, and which field, I used a LINQ Expression. The Expression performed the sorting, and then I simply bound those results to my gridview.
I suspect the jQuery method would be faster, and wouldn't require a full postback.
Correct - you will need to handle the onsorting, sort your list and re-bind.
Alternatively you could look at handling the sorting client side using a javascript framework like jQuery.
You could write a Compare for your objects:
Then when in your sorting event just pass in the Compare function into your objects sort routine (assuming you have something like List).
You now have a sorted list so just rebind it.
Correct, you need to handle the OnSorting event and set the AllowSorting property to true.