views:

3534

answers:

8

I am attemping to have my gridview be:

  • bound by a List 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 SortExpressions are all set correctly.

The resulting error message:

The GridView 'myGridView' fired event Sorting which wasn't handled.

How can I have my List sorted?

I am suspecting that it will have to do with specifying a function for the OnSorting attribute.

 OnSorting="MySortingMethod"
+2  A: 

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.

brendan
+1  A: 

You could write a Compare for your objects:

private int CompareObject(YourObject object1, YourObject object2)
{
    int iReturnValue = 0;
    if ((object1 != null) && (object2 != null) &&
        (object1.SomeField != object2.SomeField))
    {
        iReturnValue = (object1.SomeField > object2.SomeField) ? 1 : -1;
    }
    return iReturnValue;
}

Then when in your sorting event just pass in the Compare function into your objects sort routine (assuming you have something like List).

// Your list of data from the session or viewstate or whereever you have it stored.
lstObjects.Sort(CompareObject);

You now have a sorted list so just rebind it.

Kelsey
+1  A: 

Correct, you need to handle the OnSorting event and set the AllowSorting property to true.

Michael Kniskern
+2  A: 

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.

 using System.Linq.Expressions;

     protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
            {
                //re-run the query, use linq to sort the objects based on the arg.
                //perform a search using the constraints given 
                //you could have this saved in Session, rather than requerying your datastore
                List<T> myGridResults = PerfomSearch();


                if (myGridResults != null)
                {
                    var param = Expression.Parameter(typeof(T), e.SortExpression);
                    var sortExpression = Expression.Lambda<Func<T, object>>(Expression.Convert(Expression.Property(param, e.SortExpression), typeof(object)), param);


                    if (e.SortDirection == SortDirection.Ascending)
                    {
                       myGridView.DataSource = myGridResults.AsQueryable<T>().OrderBy(sortExpression);
                    }
                    else
                    {
                        myGridView.DataSource = myGridResults.AsQueryable<T>().OrderByDescending(sortExpression);
                    };


                    myGridView.DataBind();
                }
            }
p.campbell
A: 

WOW! Perfect! That's exactly what i'm looking for (and turn around on about 20 sites before getting this exemple)! And it works just fine exept for a things. I'm using it with Entity framework and now i need to go through one step over properties. Like : Customer.Contry.CountryName. Hope you can help me out cuz i'm stunk! Thank!

I've also a problem with the "List" ... it aint working. Then i've "hard coded" my entity in it but it could be great if i could use it everywhere in something like "utility" external class.

P.s.: Sorry for my english... i'm frenchiiieee :oP!

Simon
Merci Simon. Veuillez laisser une voix à la question et réponse si vous aimez.
p.campbell
Im new with this site. But i've got to say that its seems realy usefull. But what do you mean? I preaty appreciate the fact that you write this in french, but you can write in english. I fully understand english but i still have difficulties to write it. Thanks. Translate "Merci Simon. Veuillez laisser une voix à la question et réponse si vous aimez" in english :).
Simon
@p.campbell: Thanks for the effort of writing French. That is rare to see in such a forum as SO, particularly since one of the rule is to write English only. Thanks!
Will Marcouiller
A: 

Hello,

I am getting the below error when trying to sort the data i.e. List

"the datasource does not support server side paging"

Please help me to solve out this problem.

Many Thanks

baldev
A: 

thanks you, it works for me :)

huydo
A: 

this piece of code not working... Allow paging ="true" in grid... Please do suggest

lancer
I mean if allowpaging="true" in a grid. then it gives following error : " the datasource does not support server side paging"
lancer