Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

GridView paging

GridView paging in asp.net:

In this article I will explain how to apply paging on inbuilt asp.net GridView control.

ASPX Code:

<asp:GridView ID="grdUserUpdateHistory"
                runat="server"
                AllowPaging="True"
                AutoGenerateColumns="False"
                GridLines="Horizontal"
                OnPageIndexChanging="grdUserUpdateHistory_PageIndexChanging"
                CssClass="contentTable">
                <RowStyle CssClass="RowBrder"  />
                <AlternatingRowStyle CssClass="altRow"></AlternatingRowStyle>
                <Columns>
 
                    <asp:BoundField HeaderText="UpdateDetail " DataField="UpdateTitle"></asp:BoundField>
                    <asp:BoundField HeaderText="Version" DataField="Version"></asp:BoundField>
                    <asp:BoundField HeaderText="UpdatedDate" DataField="ModifiedDate"></asp:BoundField>
                    <asp:BoundField HeaderText="Modified By " DataField="ModifiedByUser.UserName"></asp:BoundField>
         
                </Columns>
                <PagerStyle CssClass="css-pageing " />
            </asp:GridView>
 

C# code:

Girdview PageIndexChanging event:

        protected void grdUserUpdateHistory_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //paging
            if (!string.IsNullOrEmpty(hnUserId.Value))
            {
                var UserID= Convert.ToInt32(hnUserId.Value);
                fillGrid(UserID);
             
            }
            grdUserUpdateHistory.PageIndex = e.NewPageIndex;
            grdUserUpdateHistory.DataBind();
        }

 

Created Fillgrid method that retrieves user record modified data

public void fillGrid(int pageID)
        {
            var UserHistoryList =UserBL.GetHistoryData(UserID);
             
                grdUserUpdateHistory.DataSource = UserHistoryList;
        }