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
Solved

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

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Change to file doesn't show up 16 51
Please explain purpose of GZIP 4 34
Input parameteres to DragOver 2 19
Unlocking a column in excel using C# 17 15
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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

809 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