SQL Server - Need help in dynamically load gridview

Asked By Sumit Kumar on 29-Jan-10 12:20 AM
Hi all,

Pls somebody give me the solutions. Suppose i have data in database table. I wanna display all the field with data in gridview. In database table i have 1000 rows of data. But in gridview in one page can hold only 50 rows. So when i click on page 2 in gridview it should display next 50 rows....then again if click on page 3, gridview should display next 50 rows...like this it will continue. I don't want all the rows(1000 rows) to be loaded at a time. It should load dynamically everytime 50 rows only.. Pls give me the solution, how do i achieve it with code..

Thanks,

Sumit

Kalit Sikka replied to Sumit Kumar on 29-Jan-10 12:27 AM
  1. <asp:GridView
  2. ID="PagedGridView"
  3. AllowPaging=true
  4. PageSize=50
  5. EnableViewState=false
  6. runat="server">
  7. </asp:GridView>
Refer: http://www.nikhedonia.com/notebook/entry/efficient-paging-for-gridview/
Sakshi a replied to Sumit Kumar on 29-Jan-10 12:53 AM

use stored procedures for paging and that will be faster and the best solution for grid view paging.

see the below link on how to:

http://msdn.microsoft.com/en-us/library/k10148y1(VS.80).aspx


Thanks and Regards,
http://www.CodeCollege.NET
http://www.InterviewsGuru.info


Huggy Bear replied to Sumit Kumar on 29-Jan-10 01:31 AM
You need to pass the current page number and the page size of the GridView to the stored procedure and should make use of that in the SELECT statement's WHERE clause.

Here is a sample select stored procedure 

CREATE PROCEDURE get_data(@pageIndex int,
@PageSize int,
@OutParameter int out
)
AS

Select * from 
(
select *,ROW_NUMBER() over(Order By Name) as PageNum
from yourtable
as yourtable
where PageNum Between (@PageIndex - 1) * @PageSize + 1 and @PageIndex * @PageSize

select @outParameter=count(*) from yourtable

This stored procedure should be called  and the data should be binded in the GridView1_PageIndexChanged event.

//gets the page index
int pageIndex = GridView1.CurrentPageIndex;
//gets the page size
int pageSize = GridView1.PageSize;
mv ark replied to Sumit Kumar on 29-Jan-10 02:20 AM
Implement the Continuous Scrolling UI Pattern in ASP.NET to dynamically load records into gridview - http://www.eggheadcafe.com/tutorials/aspnet/b8381915-06d9-4538-b4bb-5ac2a8e73f34/implementing-continuous-s.aspx