Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

manual gridview paging

Tags:

asp.net

I’m am pulling 10 out of 100 records back from the database and placing into gridview (no datasource objects here).

How do I enable paging that comes with the gridview? I know the total records is 100 can I use that somehow to activate the paging?

I know I can do this easily with DataSource objects but was just wondering if I could do it completely manually as far as the GridView is concerned.

Markup

<form id="form1" runat="server">
    <div>
    
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            AllowSorting="True" onpageindexchanging="GridView1_PageIndexChanging" 
            onsorting="GridView1_Sorting">
        </asp:GridView>
    
    </div>
</form>

Code-behind

protected void Page_Load(object sender, EventArgs e)
{
    GridView1.DataSource = GetCustomers();
    GridView1.DataBind();
}
like image 499
Rod Avatar asked May 22 '26 22:05

Rod


1 Answers

Try this:

protected void Page_Load(object sender, EventArgs e)
{
   if(!Page.IsPostback) {BindData();}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
     GridView1.DataSource = GetCustomers();
     GridView1.PageIndex = e.NewPageIndex;
     GridView1.DataBind();

}

void BindData()
{
   GridView1.DataSource = GetCustomers();
   GridView1.DataBind();

}

Also you need to add this to gridview markup:

OnPageIndexChanging="GridView1_PageIndexChanging"
like image 114
rs. Avatar answered May 25 '26 13:05

rs.