Add a datagrid to your aspx page.
Set the following properties in the designer:
AutoGenerateColumns="false"
        AllowPaging="true"
        AllowCustomPaging="true"
        AllowSorting="false"

<asp:DataGrid
        ID="dg"
        AutoGenerateColumns="false"
        AllowPaging="true"
        AllowCustomPaging="true"
        AllowSorting="false"
        DataKeyField="Id"
        OnPageIndexChanged="dgZendingen_PageIndexChanged"
        runat="server"
        PageSize="20">

Add the pagerstyle to the datagrid
<PagerStyle PageButtonCount="10" Mode="NumericPages"></PagerStyle>

Add the following events

protected void dgZendingen_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            dgZendingen.CurrentPageIndex = e.NewPageIndex;
            this.BindDataGrid();
        }

And Functions

        public void BindDataGrid()
        {

            int startindex = 1;
            if (dg.CurrentPageIndex > 0)
            {
                    startindex = (dg.PageSize * dg.CurrentPageIndex) + 1;
            }
            else
            {
                startindex = 1;
            }
            ds = // get subset of items with sp pSubSetItemsSelect;
            if (dg.CurrentPageIndex == 0)
            {
                    dg.VirtualItemCount = // get total item count with sp pTotalCount;
            }
            
                        

            DataView dv = ds.Tables[0].DefaultView;
            dgZendingen.DataSource = dv;
            dgZendingen.DataBind();
        }

Create 2 stored procedures, the first to calculate the total item count and the second to get the subset of items

First sp
if exists (select * from dbo.sysobjects where name = ‘pTotalCount’ and xtype = ‘p’)
    drop procedure pTotalCount
go

create    procedure pTotalCount
as
begin

    declare @totalCount int
    set @totalCount = 0        
                                            
    select @totalCount = count(1)
    from
        Table1 t (nolock)
                                
end
go

Second Sp


if exists (select * from dbo.sysobjects where name = ‘pSubSetItemsSelect’ and xtype = ‘p’)
    drop procedure pSubSetItemsSelect
go

create    procedure pSubSetItemsSelect
    @MaxItemsOnPage int = 20,
    @StartIndex int = 0
as
begin

    select DerivedTable.RowNum, DerivedTable.Id, DerivedTable.[Message]
    from
         (
                select row_number() over(order by [Message]) as RowNum, Table1.Id, Table1.[Message]
                from
                    Table1 t (nolock)
            ) as DerivedTable
    where RowNum between @StartIndex and (@StartIndex + @MaxItemsOnPage) 1
                                
end
go

One Comment

  1. Hello Sir,
    This is not a example with GridView control. Because AllowCustomPaging and VirtualItemCount features are not yet supported in Asp.NET GridView control till framework 4.5. With Framework 4.5 both the features are available with GridView control. It was a pain to implement custom paging in GridView control before Framework 4.5.

    Vijay

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.