我正在使用一个GridView控件,并手动进行分页和排序。 下面是分页的方法:
protected void gdvMainList_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gdvMainList.PageIndex = e.NewPageIndex;
gdvMainList.DataSource = dtConsentReleaseList;
gdvMainList.DataBind();
}
我有一个静态数据表,其中有一列Id:
dtConsentReleaseList.Columns.Add("Id");
dtConsentReleaseList.Columns.Add("StartDate");
dtConsentReleaseList.Columns.Add("EndDate");
dtConsentReleaseList.Columns.Add("Contact");
我在GridView中分配了数据关键字“Id”。 每一行都有一个打印按钮。当我点击该按钮时,执行以下代码:
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);
}
但是当我切换页面并点击打印按钮时,该行代码会出现异常:
int consentReleaseId = Convert.ToInt32(gdvMainList.DataKeys[selectedIndex].Value);
异常是:
Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index
我猜在分页方法中我做错了什么。请帮帮我吧?
CommandArgument='<%# Eval("Id") %>'
来存储ID而不是索引。 - Josh M.