Friday 29 October 2010

SPDataGrid error 'Unable to cast object of type int32 - SPDataGrid.NewPageIndex property and DataKeyNames

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

2 comments:

Anonymous said...

This is not acceptable. Any workaround for this without having to remove DataKeyNames in Sharepoint 2010?

Markus

Anonymous said...

For some reason SharePoint assumes that all DataKeys are strings. Therefore, it is enough to make sure that the DataKeys you use are all strings (rather than integers, as in the case that is causing the problem). You do not need to stop using DataKeys altogether. --GTK

Post a Comment