SharePoint - OnPageIndexChanging - Asked By Bookworm on 12-Jan-12 05:01 AM

Hi,

im working on a SPGridView, and i need to set paging
im writing this

voidgrid_PageIndexChanging(objectsender, GridViewPageEventArgs e)
      {
        grid.PageIndex 
=e.NewPageIndex;
        grid.DataBind();
      }

but when i try to go to the page 2(which has data)
it throws an error: Unable to cast object of type 'System.Int32' to type 'System.String'."}
any help?

thnx
kalpana aparnathi replied to Bookworm on 12-Jan-12 05:08 AM
hi,


The problem was in your ascx control file where you defined DataKeys and didn't use it anywhere. please  simply removed DataKeys and it resolved  your the problem.
Suchit shah replied to Bookworm on 12-Jan-12 05:09 AM
Here in this

grid.DataBind();


why you call this you need to call the method to bind the Datagridview again... say you have right the method called Bind grid in which you bind the Griddata so you need to call that

see like below one

Protected Void BindGrid()
{
       dtFillGrid = new DataTable();
      objDc = new DMS_BAL.DataClass();
      dtFillGrid = objDc.SERACHBYDATACLASS("SERACHBYDATACLASS.GET_DATA_APPROVE", DataClass, strWhere, ViewState["UserID"].ToString(), "0");
      gvFldVal.DataSource = dtFillGrid;
      gvFldVal.DataBind();
}
 
 
Protected Boid gvFldval_PageIndexChanging( object sender, GridviewPageEventArgs e)
{
   gvFldval.PageIndex = e.newpageIndex;
  BindGrid();
}


As you have seen in page index change event i just bind the gridview again so corrtect you statemetn
kalpana aparnathi replied to Bookworm on 12-Jan-12 05:11 AM
Problem: SPDataGrid can't use DataKeyNames, it results in the error "Unable to cast object of type 'System.Int32' to type 'System.String'." when paging or filtering.


Hypothesis: This looks like a bug in the implementation of the SPGridView.  The property DataKeyNames causes the error.

Resolution: To pass values using buttons or check boxes inside you SPGridView rather use the row index to get the row and read the Id or data you need from the row.

More info:
http://www.sharepointoverflow.com/questions/4481/using-code-compiled-for-moss-2007-in-2010
Bookworm replied to kalpana aparnathi on 12-Jan-12 05:22 AM
thnx kalpana it works now,
but it does not shows the items that has to be shown in the second page?
any idea?

thnx
dipa ahuja replied to Bookworm on 12-Jan-12 05:25 AM
You haven't rebind the gridview

Here is the simple example of gridview paging:
 
Set the AllowPaging = True for gridview and write this code in codebehind
 
protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack)
   {
     bindGrid();
   }
}
void bindGrid()
{
   SqlDataAdapter da = new SqlDataAdapter("select * from TableName", "Connection String");
   DataTable dt = new DataTable();   
  da.Fill(dt);
   GridView1.DataSource = dt;
   GridView1.DataBind();
}
 
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
   GridView1.PageIndex = e.NewPageIndex;
   BindGrid();
   //Bind Grid
}
 
 

Bookworm replied to dipa ahuja on 12-Jan-12 05:34 AM
thnx a lot it was very useful
dipa ahuja replied to Bookworm on 12-Jan-12 05:40 AM
Your Welcome :)