Reputation: 1096
How to perform sorting operation for a grid view .I have done this(below code) but it is throwing following error.
Error:
the type arguments for method 'System.Linq.Enumerable.OrderBy<TSource,TKey>(System.Collections.Generic.IEnumerable<TSource>,
System.Func<TSource,TKey>, System.Collections.Generic.IComparer<TKey>)'
cannot be inferred from the usage.
code:
gridview1.DataSource = (from bk in bookList
join ordr in bookOrders
on bk.BookID equals ordr.BookID
select new
{
BookID = bk.BookID,
BookNm = bk.BookNm,
PaymentMode = ordr.PaymentMode
}).AsQueryable().OrderBy(e.SortExpression,e.SortDirection);
Upvotes: 0
Views: 345
Reputation: 1096
at last I have done the following
protected void gridview1_Sorting(object sender, GridViewSortEventArgs e)
{
int p = int.Parse(Session["p"].ToString());
p++;
Session["p"] = p;
if (p % 2 == 0)
{
var A = from bk in bookList
join ordr in bookOrders
on bk.BookID equals ordr.BookID
select new
{
BookID = bk.BookID,
BookNm = bk.BookNm,
PaymentMode = ordr.PaymentMode
};
gridview1.DataSource = A.OrderBy(x => x.BookID);
gridview1.DataBind();
}
else
{
var A = from bk in bookList
join ordr in bookOrders
on bk.BookID equals ordr.BookID
select new
{
BookID = bk.BookID,
BookNm = bk.BookNm,
PaymentMode = ordr.PaymentMode
};
gridview1.DataSource = A.OrderByDescending(x=>x.BookID);
gridview1.DataBind();
}
}
Upvotes: 0
Reputation: 17804
You are mixing string-based sorting mechanism (GridView.Sorting event) with Linq (lambda expressions based sorting).
This article: Handle GridView.OnSorting() and create sorting expression dynamically using LINQ does what you want
Upvotes: 1