Fetch data from database once on pageload and load it in a DataTable. Keep this datatable as a global variable and bind the Gridview with this table in PageIndexChanging event.
DataTable myDataTable=FetchData(connectionString,query);
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
//Code to bind datasource
GridView1.DataSource=myDataTable;
GridView1.DataBind();
}
}
protected DataTable FetchData(connectionString,query)
{
//Code to Fetch Data
}
Please mark this post as answer if you find this useful!
Priya Darshini
@ProgrammingFree
we can have 1000 records in the table ,if load that all records in single bind it will cause the page load issue, to avoid this issue you can fetch the records based on page index and page size ,to achive this you need to write the Stored
procedure like following
CREATE PROC [dbo].[SP_Table]
@PageIndex int,
@PageSize int,
@Result int out ,
@status int
AS
BEGIN
SET NOCOUNT ON
DECLARE
@Value1 int
,@Value2 int
,@Value3 int
,@Value4 int
,@Value5 int
,@Value6 int
set @Value1=@PageSize
set @Value2=@PageIndex
set @Value3=@Value1 * @Value2
set @Value4=@Value3-@Value1
set @Value5=@Value4+1
set @Value6=@Value3
IF @Status = 0 OR @Status = 1
BEGIN
;with abcd
as
(
SELECT
[ID],
[Name],
[Status],
ROW_NUMBER() over (order by ID ) as RowNumber
FROM MT_Table where [Status]=@Status
)
select * from abcd where RowNumber between @Value5 and @Value6
END
ELSE
BEGIN
;with abcd
as
(
SELECT
[ID],
[Name],
[Status],
ROW_NUMBER() over (order by ValuesetID ) as RowNumber
FROM MT_Table
)
select * from abcd where RowNumber between @Value5 and @Value6
END
IF @Status = 0 OR @Status = 1
BEGIN
Select @Result = COUNT(*) from MT_Table where Status=@Status
END
ELSE
BEGIN
Select @Result = COUNT(*) from MT_Table
END
Print (@Result)
END
soumya966.ku...
0 Points
33 Posts
On page index bind gridview
Apr 05, 2012 04:10 AM|LINK
Hi,
Can anybody help me to load gridview on page index xhange.
On every pageindex change, want to call sql query to get records from database.
Please help me on this
Regards,
Soumya,
Bangalore.
Dhinadotnet
Member
544 Points
204 Posts
Re: On page index bind gridview
Apr 05, 2012 04:16 AM|LINK
I guess you are trying to do custom pagination, Go though the below link it will give clear idea
http://www.codeproject.com/Articles/4228/Custom-Data-Paging-in-ASP-NET
On each page number click it will call the Stored Procedure and display the record in GridView.
Hope this will help you.
me_ritz
Star
9337 Points
1447 Posts
Re: On page index bind gridview
Apr 05, 2012 05:05 AM|LINK
Refer to this link:
http://itdeveloperzone.blogspot.in/2011/01/custom-paging-in-grid-view.html
Thanks
soumya966.ku...
0 Points
33 Posts
Re: On page index bind gridview
Apr 05, 2012 11:23 AM|LINK
Hi,
Thanks for the reply.But in my case we may use msAccess, sql oracle any database.
Want to run inline query to fetch the records.
Any other solution?
Regards,
Soumya,
Bangalore
programmingf...
Member
406 Points
72 Posts
Re: On page index bind gridview
Jan 16, 2013 08:05 PM|LINK
Fetch data from database once on pageload and load it in a DataTable. Keep this datatable as a global variable and bind the Gridview with this table in PageIndexChanging event.
DataTable myDataTable=FetchData(connectionString,query); protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; //Code to bind datasource GridView1.DataSource=myDataTable; GridView1.DataBind(); } } protected DataTable FetchData(connectionString,query) { //Code to Fetch Data }Priya Darshini
@ProgrammingFree
AarthiPalani...
Member
61 Points
35 Posts
Re: On page index bind gridview
Jan 17, 2013 09:14 AM|LINK
Hi Friend,
we can have 1000 records in the table ,if load that all records in single bind it will cause the page load issue, to avoid this issue you can fetch the records based on page index and page size ,to achive this you need to write the Stored procedure like following
CREATE PROC [dbo].[SP_Table] @PageIndex int, @PageSize int, @Result int out , @status int AS BEGIN SET NOCOUNT ON DECLARE @Value1 int ,@Value2 int ,@Value3 int ,@Value4 int ,@Value5 int ,@Value6 int set @Value1=@PageSize set @Value2=@PageIndex set @Value3=@Value1 * @Value2 set @Value4=@Value3-@Value1 set @Value5=@Value4+1 set @Value6=@Value3 IF @Status = 0 OR @Status = 1 BEGIN ;with abcd as ( SELECT [ID], [Name], [Status], ROW_NUMBER() over (order by ID ) as RowNumber FROM MT_Table where [Status]=@Status ) select * from abcd where RowNumber between @Value5 and @Value6 END ELSE BEGIN ;with abcd as ( SELECT [ID], [Name], [Status], ROW_NUMBER() over (order by ValuesetID ) as RowNumber FROM MT_Table ) select * from abcd where RowNumber between @Value5 and @Value6 END IF @Status = 0 OR @Status = 1 BEGIN Select @Result = COUNT(*) from MT_Table where Status=@Status END ELSE BEGIN Select @Result = COUNT(*) from MT_Table END Print (@Result) ENDC# Code
protected void grd_PageIndexChanged(object sender, Telerik.Web.UI.GridPageChangedEventArgs e) { int PageSize = radgrdValueset.PageSize; int PageIndex = e.NewPageIndex + 1; BindValueSets(PageSize ,PageIndex ); }Aarthi