GridView Swap Issues

I use the gridview control and do the paging and sorting manually. Here is the paging method:

protected void gdvMainList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gdvMainList.PageIndex = e.NewPageIndex;
        gdvMainList.DataSource = dtConsentReleaseList;
        gdvMainList.DataBind();
    }

I have a static datatable having an Id column:

dtConsentReleaseList.Columns.Add("Id");
            dtConsentReleaseList.Columns.Add("StartDate");
            dtConsentReleaseList.Columns.Add("EndDate");
            dtConsentReleaseList.Columns.Add("Contact");

I assign datakeynames "Id" in my GridView. And I also have a print button on each line. When I click this button, this code runs:

else if (e.CommandName == "New")
        {                
            int selectedIndex = Convert.ToInt32(e.CommandArgument);
            int consentReleaseId = Convert.ToInt32(gdvMainList.DataKeys[selectedIndex].Value);
            string openReportScript = Utility.OpenReport(ResolveClientUrl("~/Reports/Consumer/ConsentReleaseReport.aspx?Id=" + consentReleaseId + "&ReportTitle=ConsentForRelease"));
            ScriptManager.RegisterClientScriptBlock(upConsentRelease, upConsentRelease.GetType(), "Pop up", openReportScript, true);
        }

but when I change the page and press the print button, an exception is raised on this line:

int consentReleaseId = Convert.ToInt32(gdvMainList.DataKeys[selectedIndex].Value);

An exception:

Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index

I think I'm doing something wrong in the paging method.

Any help please?

+3
source share
3 answers

. . DataKeys, . CommandArgument.

<asp:ImageButton CommandName="New" CommandArgument='<%# Eval("Id") %>' ID="ibtnPrint" runat="server" ImageUrl="~/App_Themes/Default/images/print.png" />

:

int consentReleaseId = int.Parse(e.CommandArgument);
+1

, gridview (, page_load) .

, Id CommandArgument. , , . (GridView CommandArgument )

0

datakeyname gridview.

,

string[] dk = new string[1] {"MyID"};
myGridView.DataKeyNames = dk;
myGridView.DataSource = ds;
myGridView.DataBind();
0

All Articles