well, I was getting the same error and what fixed it for me was setting AllowPaging and AllowSorting to false on the GridView. If you want to keep these properties intact like I did, then what I did was instatiate another GridView and bindData() to new GridView (it will be a temp copy without paging and sorting allowed). Then I export the new GridView. Here is a code sample:
Protected Sub ButtonExportData_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonExportData.Click
Dim stringWrite As New System.IO.StringWriter()
Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
Dim gridViewExportTable As New GridView
gridViewExportTable.DataSource =
Me.AccessDataSourceGetTable
gridViewExportTable.DataBind()
Response.Clear()
Response.Charset =
""
Response.ContentType =
"application/vnd.ms-excel" ' set the content type
Response.AddHeader(
"Content-Disposition", " attachment; filename=MyRentNetGrd_Report.xls")
Page.EnableViewState =
False
gridViewExportTable.RenderControl(htmlWrite)
Response.Write(stringWrite.ToString())
Response.End()
End Sub