I have came across your question on this forum. I had also encountered with the same problems for the number of times in the past and I found that it occurs when you have explicitly EnablePaging or EnableSorting set to true value.
The fix for this is to disable the sorting and the paging before your export and also make sure to re enable them after your export is done.
The above error can also occur when you want to export your GridView control to the Word, Excel, or to the any other formats. Your compiler will assume that your control is not added to your form and is rendered without it and so it will throw above error even if the GridView control is inside the form with the runat = “server”.
Now you must convey to your compiler that your control is rendered explicitly by overriding the VerifyRenderingInServerForm event.
As shown below:
public override void VerifyRenderingInServerForm(Control control)