Sort a GridView thats bound to a Linq query
I've got a GridView that I populate using a Linq query
var usersToApprove = from rl in QVDash.Permisisons
join u in QVDash.Userson rl.UserId equals u.UserId
join d in QVDash.Dashboards on rl.DashboardId equals d.DashboardId
where (u.ApprovedBy == approverId || isGod)
select new
{
PermissionId = rl.PermissionId,
Name = u.Name,
Area = u.Area,
Dashboard = d.DashboardName,
OpUnit = rl.Operational_Unit,
Cost_Centre = rl.Cost_Centre,
Fund = rl.Fund,
Project = rl.Project,
Approver = (from a in QVDash.Users where a.UserId == u.UserId && a.UserId == u.ApprovedBy select a.Name).FirstOrDefault()
};
grd开发者_StackOverflow_CurrentUsers.DataSource = usersToApprove ;
grd_CurrentUsers.DataKeyNames = new string[] { "PermissionId" };
grd_CurrentUsers.DataBind();
This however presents an issue, where the GridView will not allow sorting.
I've tried a few options that I've found which resort to building a custom sorting function, however I've found this to be sketchy at best, which issues such as: Only one way sorting, Paging resets sort etc.
What I'm wanting to know is if there is a better way to do this.
I know that binding the gridview to a datasource takes care of the sorting issue, however I don't know how to bind my Linq to a data source. Is it possible?
Have you tried the LinqDataSource control? Check out this link. http://weblogs.asp.net/scottgu/archive/2007/09/07/linq-to-sql-part-9-using-a-custom-linq-expression-with-the-lt-asp-linqdatasource-gt-control.aspx
Key part: "To handle custom query scenarios you can implement an event handler to handle the "Selecting" event on the control. Within this event handler you can write whatever code you want to retrieve a data model result."
This allows you to sort, page, etc, with your custom LINQ query.
Finally figured it out with the sorting code from this site http://forums.asp.net/t/1368247.aspx
精彩评论