Solved

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

Posted on 2011-02-14
2
536 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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 video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

759 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

18 Experts available now in Live!

Get 1:1 Help Now