Solved

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

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Code enhancement 4 34
Need help converting bitmap to image in VB.Net 8 46
locate sql commands in C# visual studio Project 6 45
VB.net/VSTO Excel Add-in 2 19
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 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