C# 如果编辑行返回此错误,请更改GridView上的页面
我知道stackoverflow中存在这个问题。但我的似乎不一样。我看没什么问题。但当gridview上的页面发生更改时,有时会在运行时发生这种情况 如果在Gridview的第一页上工作,我在Gridview的编辑行上没有错误。 如果更改Gridview的页面并尝试编辑任何行返回错误 我正在尝试将数据一行一行地添加到datagridview中,这是我的代码,上面写着: “索引超出范围。必须为非负且小于大小。” 集合参数名称的“索引” 这意味着什么?我的代码有问题吗 线路错误: GridView g2= (GridView)gvProducts.Rows[rowindex].FindControl(“GridView2”) 这是我的密码:有人能看到并告诉我发生了什么吗C# 如果编辑行返回此错误,请更改GridView上的页面,c#,gridview,pagination,C#,Gridview,Pagination,我知道stackoverflow中存在这个问题。但我的似乎不一样。我看没什么问题。但当gridview上的页面发生更改时,有时会在运行时发生这种情况 如果在Gridview的第一页上工作,我在Gridview的编辑行上没有错误。 如果更改Gridview的页面并尝试编辑任何行返回错误 我正在尝试将数据一行一行地添加到datagridview中,这是我的代码,上面写着: “索引超出范围。必须为非负且小于大小。” 集合参数名称的“索引” 这意味着什么?我的代码有问题吗 线路错误: GridView
protected void gvProducts_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName != "Page")
{
int rowindex = Convert.ToInt32(e.CommandArgument.ToString());
GridView g2 = (GridView)gvProducts.Rows[rowindex].FindControl("GridView2");
if (e.CommandName == "Details")
{
int customerId = (int)this.gvProducts.DataKeys[rowindex]["sID"];
gvProducts.Rows[rowindex].FindControl("btn_Show").Visible = false;
sql = @String.Format(" SELECT * FROM `doTable` ");
sql += String.Format(" WHERE ");
sql += String.Format(" sID IN ('{0}') ", customerId);
g2.DataSource = GetData(sql);
g2.DataBind();
g2.Visible = true;
}
else
{
g2.Visible = false;
gvProducts.Rows[rowindex].FindControl("btn_Show").Visible = true;
}
}
}
protected void Paginate(object sender, CommandEventArgs e)
{
int intCurIndex = gvProducts.PageIndex;
switch (e.CommandArgument.ToString().ToLower())
{
case "First":
gvProducts.PageIndex = 0;
break;
case "Prev":
gvProducts.PageIndex = intCurIndex - 1;
break;
case "Next":
gvProducts.PageIndex = intCurIndex + 1;
break;
case "Last":
gvProducts.PageIndex = gvProducts.PageCount - 1;
break;
}
gvProducts.DataBind();
}
<PagerTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="/aspnet/img/bot_back.gif"
CommandArgument="First" CommandName="Page" />
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="/aspnet/img/bot_back2.gif"
CommandArgument="Prev" CommandName="Page" />
Page
<asp:DropDownList ID="ddlPages" runat="server" AutoPostBack="True" CssClass="ddl_Class"
OnSelectedIndexChanged="DDLPages_SelectedIndexChanged">
</asp:DropDownList>
of
<asp:Label ID="lblPageCount" runat="server"></asp:Label>
<asp:ImageButton ID="ImageButton3" runat="server" ImageUrl="/aspnet/img/bot_next.gif"
CommandArgument="Next" CommandName="Page" />
<asp:ImageButton ID="ImageButton4" runat="server" ImageUrl="/aspnet/img/bot_next2.gif"
CommandArgument="Last" CommandName="Page" />
</PagerTemplate>
protectedvoid gvProducts\u行命令(对象发送方,GridViewCommandEventArgs e)
{
如果(如CommandName!=“第页”)
{
int rowindex=Convert.ToInt32(例如,CommandArgument.ToString());
GridView g2=(GridView)gvProducts.Rows[rowindex].FindControl(“GridView 2”);
如果(如CommandName==“详细信息”)
{
int customerId=(int)this.gvProducts.DataKeys[rowindex][“sID”];
gvProducts.Rows[rowindex].FindControl(“btn_-Show”).Visible=false;
sql=@String.Format(“从'doTable'中选择*”;
sql+=String.Format(“其中”);
sql+=String.Format(“sID IN({0}')”,customerId);
g2.DataSource=GetData(sql);
g2.DataBind();
g2.可见=真实;
}
其他的
{
g2.可见=假;
gvProducts.Rows[rowindex].FindControl(“btn_-Show”).Visible=true;
}
}
}
受保护的无效分页(对象发送方,CommandEventArgs e)
{
int intCurIndex=gvProducts.PageIndex;
开关(例如CommandArgument.ToString().ToLower())
{
案例“第一”:
gvProducts.PageIndex=0;
打破
案例“Prev”:
gvProducts.PageIndex=intCurIndex-1;
打破
案例“下一个”:
gvProducts.PageIndex=intCurIndex+1;
打破
“最后”一案:
gvProducts.PageIndex=gvProducts.PageCount-1;
打破
}
gvProducts.DataBind();
}
页
属于
编辑#01
<asp:TemplateField HeaderText="" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:ImageButton ID="btn_Show" runat="server"
CommandName="Details"
CommandArgument='<%#Container.DataItemIndex%>' />
</ItemTemplate>
</asp:TemplateField>
您需要更换此:
<asp:TemplateField HeaderText="" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:ImageButton ID="btn_Show" runat="server"
CommandName="Details"
CommandArgument='<%#Container.DataItemIndex%>' />
</ItemTemplate>
</asp:TemplateField>
与:
<asp:TemplateField HeaderText="" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:ImageButton ID="btn_Show" runat="server"
CommandName="Details"
CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'/>
</ItemTemplate>
</asp:TemplateField>
您实际向方法发送了什么输入?你能举一个它崩溃的例子吗?目前看来,rowindex要么在第一页上单击“上一页”返回-1,要么太大,因为目标行还不存在?您在rowindex
变量中得到了什么值?您正在网格视图中单击哪个按钮执行gvProducts\u RowCommand
code?@ChetanRanpariyarowindex
的值总是正确的,因为我在gvProducts\u RowCommand
中添加了响应。Write(rowindex);Response.End()代码>。GridView中的执行按钮请参见我第一个问题中的编辑#01。谢谢。如果rowindex值正确,则不会出现异常。所以你需要观察它的值并在这里共享。@ChetanRanpariya我对GridView g2=(GridView)gvProducts.Rows[rowindex].FindControl(“GridView 2”)有异常代码>且不在行索引中