开发者

How do I do a Client side sort of a DataTable using LINQ

How do I sort a DataTable on the 开发者_Python百科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?


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);


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();

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜