Solved

How to use sorting in a GridView that implements custom paging?

Posted on 2011-02-14
2
542 Views
Last Modified: 2013-11-11
I am using a GridView that implements custom paging using a LinqDataSource (see code snippet).  This is working as expected, however, my sorting is no longer working.

How do you implement sorting with custom paging?

Thanks,
Mark
protected void linqDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        int pageSize = globalSalesGridView.PageSize;
        int pageIndex = globalSalesGridView.PageIndex;

        var query = from p in products
                    select p;

        e.Arguments.TotalRowCount = query.Count();

        e.Result = query.Skip(pageIndex * pageSize).Take(pageSize);
}

Open in new window

0
Comment
Question by:markerasmus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 12

Accepted Solution

by:
kumar754 earned 500 total points
ID: 34911986
download the attachment and add to your project:

see if this works for you.

using System.Linq.Dynamic;

protected void linqDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
	int pageSize = globalSalesGridView.PageSize;
	int pageIndex = globalSalesGridView.PageIndex;

	var query = from p in products
				select p;
	
	// assuming you have already set the SortExpression to your GridView Columns
	if (!string.IsNullOrEmpty(gridview.GridViewSortExpression))
	{
		query = query.AsQueryable().OrderBy(gridview.GridViewSortExpression + " " +  gridview.GridViewSortDirection);
	}

	e.Arguments.TotalRowCount = query.Count();

	e.Result = query.Skip(pageIndex * pageSize).Take(pageSize);
}

Open in new window

System.Linq.Dynamic.cs
0

Featured Post

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question