Search code examples
asp.netgridviewdata-bindingtargetpage-index-changed

PageindexChanging TargetInvocationExeption


I'm having a little bit of trouble sorting out the paging scenario with a gridview i.e. I can't get the bloody thing to show page, 2, 3, 4, etc.

I have the following Grid view code

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
               style="z-index: 1; left: 20px; top: 440px; position: absolute; height: 133px; " 
        AllowPaging="True" AllowSorting="True" Font-Size="Small" 

        PageSize="2" onpageindexchanging="GridView1_PageIndexChanging">
        <Columns>

With the following

  protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    GridView1.DataBind();
}

Now, I am getting a "TargetinvocationException was unhandled by user code."

Being a newbie, this is beyond my current capabilities and has confused me somewhat. How do I go about binding my gridview properly to allow for the paging to be operate correctly?


Solution

  • The binding was actually fine. This was finally sorted by adding in a primary key (which I really should have implmented in the first place!)

    So if anyone is reading this, cannot get their gridview to page through an objectdatasource, make sure you have a primary key!!!