views:

105

answers:

3

How do I sort a DataTable on the client side using LINQ? (Server side sorting is not supported with my data store)

I was doing something like this which doesn't work

IEnumerable<DataRow> dr = GetDataTableData().AsEnumerable();

           if (sortDirection == "Ascending")
                        {
                            dr = dr.OrderBy(x => sortExpression);
                        }
                        else
                        {
                            dr = dr.OrderByDescending(x => sortExpression);
                        }

                GridView1.DataSource = dr;
                GridView1.DataBind();

But I dont see the gridview sorting at all, what am I missing here?

A: 

I am slightly confused here as the code you have posted is server side code!

If you want client side sorting on a table then you may have to look at these two topics

  1. Ajax calls back to the server to retrieve the sorted data (in this case it is then server side)
  2. Client side sorting using a JQuery plugin
Rippo
In his world, server means the SQL (or whatever) server, and client means the application which opens a connection to the server.
Aviad P.
So why the vote down, seems a little harsh.
Rippo
In his world? I'm confused, which part of the above code is server side code?
Nevin Mathai
+2  A: 

My guess is that sortExpression is a string that you are passing in to the method; You should be sorting on something in x. eg:

dr = dr.OrderBy(x => x.FirstName);

As Rory pointed out in the comments, you can just use x[sortExpression] in your case; If you were using objects instead of the DataRow, you could make a key selector expression and pass it in to OrderBy() instead; something like:

Func<IDataRow, string> sortExpressionReal = x => x["FirstName"].ToString();

then your OrderBy would look like:

dr = dr.OrderBy(sortExpressionReal);
Chris Shaffer
Yes, sortExpression is the gridrow header the user clicks on for ex: "FirstName", How do I map that to X.FirstName?
Nevin Mathai
you could just use a linq data source and let the framework take care of it for you. Otherwise you have to write your own mapping from the values passed in to the event.
NickLarsen
Since x is a DataRow instance, you can use "x[sortExpression]" or "x.Field<TypeOfField>(sortExpression)". You will need to import the System.Data.Linq namespace to use the second version, and TypeOfField might be a problem if the fields contain different data types, the first option is easiest because it'll just treat everything as object.
Rory
Ok I tried this: dr = dr.OrderBy(x => x[sortExpression]);didnt work...still get unsorted data
Nevin Mathai
Did you update both your OrderBy and OrderByDescending? Maybe "Ascending" isn't what you are actually getting for sortDirection?
Chris Shaffer
@Chris Yes, I did...not a user error =)
Nevin Mathai
@Chris, I tried your new updated suggestions, still no sort :(
Nevin Mathai
Based on the code you have here and this update, you really should be seeing a sorted list. My only other thought is falling back to standard webforms issues; Are you possibly double-binding the list (eg, sorted bind is occurring in GridView_Sort() and an unsorted bind is occurring in Page_PreRender())?
Chris Shaffer
Yes Yes Yes...Let me try change that.
Nevin Mathai
Wait how can I fix this ?
Nevin Mathai
Quickest/easiest way would be to make sure in Page_PreRender() to only bind the gridview when !IsPostback. Alternatively, you could bind the sorted list in Page_PreRender and change GridView_Sort() so instead of binding it just sets a couple properties so PreRender knows how to sort the data.
Chris Shaffer
Thank Chris! Great Debugging :)
Nevin Mathai
A: 

Do you have to use linq. You can do this quite easily with DataViews DataView vw = new DataView(dtbl,"filter","FirstName DESC",DataViewRowState.CurrentRows); GridView1.DataSource=vw; GridView1.DataBind();

Sachin