Solved

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

Posted on 2011-02-14
2
537 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
2 Comments
 
LVL 5

Expert Comment

by:jijeesh
ID: 34891424
0
 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

930 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now