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


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

voidgrid_PageIndexChanging(objectsender, GridViewPageEventArgs e)

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?

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

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


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;
Protected Boid gvFldval_PageIndexChanging( object sender, GridviewPageEventArgs e)
   gvFldval.PageIndex = e.newpageIndex;

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:
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?

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)
void bindGrid()
   SqlDataAdapter da = new SqlDataAdapter("select * from TableName", "Connection String");
   DataTable dt = new DataTable();   
   GridView1.DataSource = dt;
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
   GridView1.PageIndex = e.NewPageIndex;
   //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 :)