how to achieve paging in a grid view in code

1.9k Views Asked by At

I'm using ASP.net web application with c#. In my web application I have a webpage with a data grid view.

I'm using this method to bind data to data grid view

 public void fillGridByALLBDetails()
    {
        GVView01.DataSource = new LibraryCatalogueOP().getLibraryCatalogue();
        GVView01.DataBind();   
    }

I'm calling the data grid view bind method in the page load event like this.

  if (!IsPostBack)
            {
                fillGridByALLBDetails();
            }

This is my business layer method to get the data.

     public DataTable getLibraryCatalogue()
{
    string myQuery1 = "EXEC SelectLibraryCatalogue";
    return new DataAccessLayer().ExecuteMyTable(myQuery1);
}

Sometimes my data grid loads a lots of data at once. I want to know how to achieve PAGING with this code. Any code example would be really great.

Thanks in advance.

3

There are 3 best solutions below

0
vallabha On BEST ANSWER
0
Brave Soul On

You can do it by using the properties

  <asp:gridview id="GVView01" 
            allowpaging="true" 
            pagesize="15"
            runat="server">

you can use pagesize="10" (or 20 whatever size of page you want to display )property of gridview in designer page and provide allowpaging="true" , in designer you can get both properties

0
Arunprasanth K V On
<asp:GridView ID="GridView1" runat="server"
    AutoGenerateColumns = "false" Font-Names = "Arial"
    Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B" 
    HeaderStyle-BackColor = "green" AllowPaging ="true"  
    OnPageIndexChanging = "OnPaging"
    PageSize = "10" >

. . .

And now in order make the paging functionality work we will need to add the OnPageIndexChanging event of the GridView control

protected void OnPaging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    GridView1.DataBind();
}