The reason why you're getting that error is because ViewState["GridViewIndex"] is not an instance of an object.
ViewState is like a dicionary, but for a given key you might get a null reference, if no object is instantiated. Please change your code to check for null references because convert it to string.
Something like
string state = string.Empty;
if(ViewState["GridViewIndex"] != null)
{
state = ViewState["GridViewIndex"].ToString();
}
or might also use Convert.ToString instead of ToString as RononDex told us, but in general we need to check for null references.