The GridView 'GridView1' fired event PageIndexChanging which wasn't handled
Asked Answered
S

5

14

I have created:

  • one master page and one content page called Detail.
  • On Button click event, displaying data in grid view.
  • In grid view, columns are autogenerated.
  • I wanted to show 11 column in grid view, but it is more than page size.

What to do for this?

I have created sql helper file for database connection code and calling that method, not using sqldatasource for connection.

When I trying to do paging, getting error:

The GridView 'GridView1' fired event PageIndexChanging which wasn't handled.

Scenography answered 30/10, 2011 at 14:52 Comment(0)
N
25

You need to declare a method on your code behind that handles the PageIndexChanging event.

Something similar to this:

protected void GridView1_PageIndexChanging (object sender, GridViewPageEventArgs  e)
{
    GridView1.PageIndex = e.NewPageIndex;
    bindGridView(); //bindgridview will get the data source and bind it again
}

private void bindGridView()
{
     GridView1.DataSource=getData();
     GridView1.DataBind();
}

Providing sample code:

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        bindGridView(); //bindgridview will get the data source and bind it again
    }

    protected void Page_Load(object sender , EventArgs e)
    {
        if(!IsPostBack)
         bindGridView();

    }
    //this is some sample data 
    private void bindGridView()
    {
        DataTable t = new DataTable();
        t.Columns.Add("Col1");
        t.Columns.Add("Col2");
        DataRow r = null;
        for (int i = 0; i < 25; i++)
        {
            r = t.NewRow();
            r.ItemArray = new object[] { "Val" + i, " Another " + i };
            t.Rows.Add(r);
        }
        GridView1.DataSource = t;
        GridView1.DataBind();
    }

And this is the markup:

<asp:GridView OnPageIndexChanging="GridView1_PageIndexChanging" AllowPaging="true" PageSize="10" ID="GridView1" runat="server" AutoGenerateColumns="true">

Produces this:

enter image description here

Neoprene answered 30/10, 2011 at 15:24 Comment(4)
ya i done that but no effect. protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { //string s1 = "select * from master"; //DataSet ds = SqlHelper.GetDataSet(CommandType.Text, s1, "master"); //DataTable table = ds.Tables["master"]; //if (table.Rows.Count > 0) //{ // GridView1.DataSource = ds; // GridView1.DataBind(); //} GridView1.PageIndex = e.NewPageIndex; //Bind grid GridView1.DataBind(); } and aspx fileScenography
@JuiTest post this code to your answer, not as a comment. It's more helpful to see it there.Neoprene
I can't answer for own question.Scenography
@JuiTest sorry, I meant add this code to your own question.Neoprene
T
3

For Paging you can use OnPageIndexChanging for this....

For Example

you have to use OnPageIndexChanging="gvdetails_PageIndexChanging" in your GridView...

You have to write below code into event in code behind like

protected void gvdetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    gvdetails.PageIndex = e.NewPageIndex;
    BindData();
}

For more detail you can check the below link here I am using page Index change in my article...

Here I use PageIndexChange

I hope this will helps you....Share it with others...Thanks!

Trencher answered 27/6, 2013 at 7:32 Comment(0)
C
1

This is the final answer:

Imports System.Collections.Generic ' library

Protected Sub grdEmployees_PageIndexChanging1(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewPageEventArgs) Handles grdEmployees.PageIndexChanging
    grdEmployees.PageIndex = e.NewPageIndex
    LoadEmployeeList() 'FUNCTION FOR DATASET
    grdEmployees.DataBind()

End Sub
Cortes answered 31/7, 2012 at 17:44 Comment(0)
L
1

you simply add this to your code :

protected void GridViewTrsEmail_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridViewTrsEmail.PageIndex = e.NewPageIndex;
    GridViewTrsEmail.DataBind();


}
Lyndonlyndsay answered 27/10, 2017 at 8:47 Comment(0)
T
0

To fix this, I had to take a closer look at my datasource and datakeys. I have a set of records that are returned from SQL Server and what I was doing is binding them to a POCO. This class had several public properties of type Integer. These Integers were my datakeys on the grid. I replaced their type with a string instead to bypass the casting issue.

Tarton answered 22/2, 2012 at 21:10 Comment(1)
This would probably have made a better comment. It doesn't add much to the accepted answer.Fca

© 2022 - 2024 — McMap. All rights reserved.