set number of pages on gridview - asp.net

I am trying to populate records on a gridview depending on the result of two linq methods:
GetRecords(string SearchCriteria, int skip, int take)
which provides me the ammount of rows i´m going to show into the gridview, and
CountRecords(string SearchCriteria)
Which provides me with the total count of the records.
First I get the number of records by using the CountRecords method, and then I calculate the number of pages to make the user know how many pages the gridview has, but the property gridview.PageCount is read only. How can i set the ammount of pages the gridview is going to have, without getting all the records (they're about 300000, so i cannot afford this option)?

Set virtual count. Set AllowCustomPaging to true.
http://msdn.microsoft.com/en-us/library/zxdbyxtc.aspx

Related

ASP.NET Depending on data number of pages

Right now my website(ASP.NET) has a database where it pulls data from, and the more data enter the longer the page. I am wondering how I can make it so for example once it pulls ten items from database it starts a new page and puts ten more items on the next. Basically like how http://fmylife.com does it.
Thanks
You should take a look att the DataPager Control.
To create a solution that works in the long run, you need to let the paging selection end up in your sql where clause, so that you only retreive the amount of records that you have specified in the PageSize property of the DataPager Control each time you round trip to the database. Otherwise it will lose it's performace when the records in the database increases.

Asp.Net : Repeater & CollectionPager Query Index Logic

I have a logical problem. Assume I have a table that includes 1000 rows. When I want to display the data in a repeater, I first bind CollectionPager 1000 rows, so CollectionPager knows how many pages there will be, and after that, CollectionPager will be my repeaters datasource.
If I index my table using Row_Number() property and select 10 records per time, and multiply it with my page_number, since its selecting 10 records only, its performance will be good. But at this time my pager does not know how many total records are there and so It does not make paging.
In the other hand I do not want to select all 1000 rows?
What do you offer me?
Thank you.
I would suggest to have one and the very first trip to database to just find out the count of total records that you will going to get, you can store it in the viewstate or hidden fields for further page post backs.
Then you can fetch 10 records per database trip and so you will always have
10 records from the database + Total records count that you can use for your pager
In this way your pager will always know the total number of records and hence will perform the paging accordingly

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.

Custom paging/sorting for GridView filled by CodeBehind Datasource?

I'm coding a report page. In this page, there are two date fields for user to filter the date and the GridView is filled depending on these dates in the CodeBehind (When user click the button view). Now I want to implement a paging/sorting feature for this GridView. I've researched and see there are default paging for GridView which is not very efficient (my report table may have thousands of records) and custom paging but this requires using ObjectDataSource (which I don't use). Thus anyone can recommend me some approaches that are best used in this situation? Some tutorials will be gladly appreciated :)
Thanks,
Assuming you have the records stored in a database, I would get the record count and use that number for paging. Then I'd query for the ones I want to see on each page.
string.Format(#"
SELECT TOP {0} * FROM Records WHERE pkId NOT IN (
SELECT TOP {1} pkId FROM Records ORDER BY pkId
) ORDER BY pkId;",
upperBoundary,
lowerBoundary
);
Where upperBoundary would be lowerBoundary + itemsPerPage, for example. Using MSSQL, we don't have the luxury of MySQL's LIMIT feature, but this does the same.
Narrowing down on dates, would be something like
SELECT * FROM Records WHERE Date > earlyDate AND Date <= lateDate
This is the most effecient way in my oppinion, cause it causes little traffic and if you cache the record count, you won't have many queries flying around either.

How to bind DataGrid to display only 25 records of a table having more than 1000 records ..?

I have a datagrid control which is bound to a table which containing more than 1000 records. And i want to show only 25 records once a time. I have used paging in datagrid. But each time the next page index is set, query is fired again. This takes lots of time. So what is the easiest way to bound data in this case to improve performance..??
I don't recommend using caching since the whole data will be returned to the server anyway for the first time.
You can improve performance by using custom paging queries to the database.
Assuming you're working with at least SQL Server 2005,
Here's a great article for your purpose with different benchmarking results
have you considered caching your data set? then you would only need to query the data if the cache is empty or expired.
When you handle your Page Changed event, you need to pull in the new page information. You will need to create a stored procedure that takes CurrentPageNumber and PageSize as arguments.
This is in addition to any other arguments you already supply when bringing down your data.
In the SP, you fill up a temporary table or table variable (or you can use a CTE) with the data that you are returning, but also the RowNumber.
Based upon your CurrentPageNumber argument, you are able to return all the results between CurrentPageNumber * PageSize and (CurrentPgaeNumber + 1) * PageSize - 1.
Here's a good resource:
How to return a page of results from SQL?

Resources