PageIndexChanging in GridView in ASP.NET
Asked Answered
L

5

27

I have a gridview which I am using to display a dataset result. The problem is I am using paging in it. But when I click on the page # it says that I haven't handled the event. Do I need to rebind the dataset???

Thanks

Lamori answered 12/1, 2011 at 5:26 Comment(0)
S
36

Try the following code:

protected void grdView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    FillGrid();
    grdView.PageIndex = e.NewPageIndex;
    grdView.DataBind();
}
Suggestive answered 12/1, 2011 at 5:30 Comment(2)
Thanks.. This works... I didn't know this pageindex incrementing before.Lamori
Viewstate should be preferred to avoid excessive db calls on page Change eventsStalwart
L
4

Try it

In the pageload

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        loadGrid();
    }
}

In the pageindexchanging

private void loadGrid()
{
    using (your_bankEntities context = new your_bankEntities()) //use your connection .edmx
    {
        var jmDados = (from jm in context.yourdbo  orderby jm.your fieldkey  
                         select new
                           {
                               jm.Field1,
                               jm.Field2,
                               jm.Field3,
                               jm.Field4,
                               ........ 
                               jm.n

                           }).ToList();
        GridView1.DataSource = jmDados;

        GridView1.DataBind();
    }
}

In the pageindexchanging

GridView1.PageIndex = e.NewPageIndex;

loadGrid();
Late answered 1/9, 2013 at 14:55 Comment(0)
A
3

You should set the .PageIndex before binding data! Otherwise, you would need extra clicks which actually double the BindData method calls. The following is my tested vb code.

Private Sub GridViewL_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridViewL.PageIndexChanging

    GridViewL.PageIndex = e.NewPageIndex
    BindData()  ' your method to bind data to the grid
End Sub
Aminaamine answered 4/3, 2019 at 2:46 Comment(0)
V
1

In VB.net, it does not have much difference with C#, you just remove the semicolons at the end of each line

Private Sub myGridview_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles myGridview.PageIndexChanging

   LoadGridView() //Call your method to load the data into the grid.
   myGridview.PageIndex = e.NewPageIndex
   myGridview.DataBind()

End Sub
Volnay answered 7/10, 2015 at 14:37 Comment(0)
I
0

In addition to other answers, keep in mind that changing the page also fires the OnRowCommand event, with CommandName "Page". Be careful not to put conflicting code in that method, that could be altering the result of your OnPageIndexChanging actions.

Indiaindiaman answered 30/3, 2023 at 21:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.