Grid View page wise sorting in asp.net - asp.net

I got here a answer to sort the gridview.
sorting and paging with gridview asp.net
But using the above solution all the grid data sorted while clicking on the any column.
I request a solution which sorts data on the particular page only.
Suppose I have 10 pages with 50 records each page, now if I visit page 7 and sort then the only 50 records of the page 7 should be sorted.

If it just matters for the current page, avoid the hassle and use a Javascript solution.
You can also do it in ASP.NET. Grab the data you want from your DataSet, after paging (so records 350 to 400 for page 7), then change your binding from something like:
// do this
gv.DataSource = pagedDataSet.OrderBy(d => d.NameOfField);
// instead of
gv.DataSource = pagedDataSet;
If you have dynamic columns, or don't want to do write this for every possible property; see my solution in this answer.

Related

asp.net + Binding DataSet to already bounded Listview

Basically what I want to achieve here is to show more items in batches just like the YouTube comments session in every vid, about 10 comments shows up for the start with a "Show More" button below which loads additional comments in batches of 10 when clicked upon. Anyone has a clue of how to go about this?
Not sure if this is right but what I intend to do now is to bind additional records to my Listview again with DataSets without extracting the data that has already been bonded.
Currently I am binding my first set of records with a Dataset filled with data from my SQL Server like this:
DataSet ds = activityBll.GetActivityAttendees(activityId);
DataTable dt = ds.Tables[0];
uilvEventParticipants.DataSource = dt;
uilvEventParticipants.DataBind();
The simplest way to do this would be to do as you suggest, and just keep getting more records from farther and farther in the past. You'll experience increasing delays as you rebind, and if you allow users to make changes to comments you'll have to do a huge postback to process those. If you go this way you should look at UpdatePanels.
The most elegant would involve jQuery or a similar Ajax technology to fetch additional records and add them to the bottom of an HTML table (not necessarily a ListView) without refreshing the page. There are quite a few questions on this board about doing that.

gridview asp.net

I have a gridview which was working fine with a small dataset in development. In production it has to bind to thousands of records, which is making it slower to load. Is there a way to improve performance, like retrieving the data during gridview pageindex changing?
Also chances are you only want to bind it once. So you should (if not already):
if(!IsPostback)
{
DatabindGridLogicHere();
}
This way your GridView will only have to hit the db the first time to get the data.
First and formost turn off ViewState.
You should tell your datasource to take less records and then enable paging in your grid and datasource.
You can enable "AllowPaging" property to true in your GridView and give a page size say 10. Then Write your data retrieval logic to return a batch of data instead of the whole set of data at once.
When you write the SQL query make sure to order it by an ID.
Thus if the page index is 1 you can take the first batch of data by passing page index of 1 and the page size of 10.
Logic will be;
SELECT [RequiredFields]
FROM [YourDataSource]
WHERE (Id>=((PageIndex-1)*pageSize) AND Id<(PageSize*pageIndex)) ORDER BY Id
In the first page it will return first set of entries of those Ids starting from 0 to 9. In the second page it returns entries of those Ids starting from 10 to 19 assuming the pageSize is 10. You can change the page size and the query logic as you wish.
But if sorting is enable then this will not produce accurate results.

Odd paging problem with DataGrid and sorting

I have a data table that is being filled by a stored procedure. I need to filter down the results some, so I get a data view from the default view and apply a row filter to it. I then bind this to my DataGrid. All is fine at this point. I see two pages (17 records with 10 per page). If i apply a sort to the grid though, it now shows 5 pages (58 records without the filter). I stepped through the code and it repopulates the data prior to running the sort. The repopulation is with with the filter in place and it counts 17 records, but shows 5 pages.
To make it even weirder, if I click on a page i know will be invalid, it runs the page change (which also repopulates the data) and this time it limits the pages to 2 and tells me i have an invalid page number!
Any ideas?
It was the custom control causing the problem. We have an AutoDataSource function that automatically pulls the data tables out of my view and sorts on them. I found a filter function in my grid that apply the filter, but only if applied before setting the datasource
Make sure to apply the sort to the default view and not the grid column directly.

Data Paging with specific layout

I am trying to use data paging (not custom data paging - just normal inefficient paging) for about max 125 records, over 5 pages, 25 records per page. But most samples out there seem to be using a gridview or a datagrid, however I want each record to look like this:
Record 1 Title
Record 1 Description
Record 1 Time: Record 1 Contact:
Record 2 Title
Record 2 Description
Record 2 Time: Record 2 Contact:
etc etc, however with grids they result is like a table with bound fields for each column so I can't display it like that using paging, I thought of using a Repeater however I think I will need to use viewstate for that as it doesn't have paging built in and don't want to use ViewState as I have disabled this - can anyone help?
I think what you are looking for is a DataView or a Repeater
The Repeater control is a basic
templated data-bound list. It has no
built-in layout or styles, so you must
explicitly declare all layout,
formatting, and style tags within the
control's templates.

asp.net custom datapager

in all the datapager examples i've seen (using a LinqDataSource tied to a ListView) the queries to the database return the full recordset. how can i get JUST the page of rows that i want to display?
for example, if i have a table with 1million rows and i want to see the 10 results on page 5, i would be getting rows 51 to 60.
i'm sure i have to implement custom paging but i haven't found any good examples.
There are many ways of doing this, however, I personally like a SQL based solution that goes to the database and gets the result set. This 4GuysFromRolla Article does a good job of explaining it.
If you're using MSSql2005, take a look at this article.
As you can see, the trick is to use the function ROW_NUMBER(), that allow you to get the sequential number of a row in a recordset. With it you can simply enable pagination based upon the number of rows you want to get in a page.
I was under the impression (from Scott Guthie's blog post "LINQ to SQL (Part 9)") that the LinqDataSource handles the paging for you at the database level:
One of the really cool things to notice above is that paging and sorting still work with our GridView - even though we are using a custom Selecting event to retrieve the data. This paging and sorting logic happens in the database - which means we are only pulling back the 10 products from the database that we need to display for the current page index in the GridView (making it super efficient).
(original emphasis)
If you are using some custom paging, you can do something like this in LINQ to SQL:
var tagIds = (from t in Tags where tagList.Contains(t.TagText) select t.TagID).Skip(10).Take(10).ToList();
This is telling LINQ to take 10 rows (equivalent to T-SQL "TOP 10"), after skipping the first 10 rows - obviously these values can be dynamic, based on the Page Size and page number, but you get the idea.
The referenced post also talks about using a custom expression with a LinqDataSource.
Scott has more information on Skip/Take in Part 3 as well.

Resources