十万万个火急~ 今天要要交付了,昨天发了一条,还是没解决,希望各位大侠帮我看看,代码写全一点。小弟在这感谢各位了!!分少了点,只能发100分的,不然把我全部身家压上!问题是这样的:
GridView绑定了N页的数据,GridView绑定的第一页可以批量修改,但只要是翻页后,修改就有报错提示:
索引超出范围。必须为非负值并小于集合大小。
参数名: index
异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。
行 113: string ID = GVjg.DataKeys[gvr.DataItemIndex].Value.ToString();
下面是代码:
<asp:GridView ID="GVjg" CellPadding="0" CellSpacing="1" runat="server"
AutoGenerateColumns="False" Width="100%"
AllowPaging="True" DataKeyNames="Jxzj_Sys_PriceID" CssClass="GridView"
GridLines="None" onpageindexchanging="GVjg_PageIndexChanging" PageSize="8">
<Columns>
<asp:BoundField DataField="xh" HeaderText="序号" ReadOnly="True" >
<ItemStyle HorizontalAlign="Center" Width="40px" />
</asp:BoundField>
<asp:BoundField DataField="dl" HeaderText="大类" ReadOnly="True" />
<asp:BoundField DataField="clmc" HeaderText="材料名称" ReadOnly="True" />
<asp:BoundField DataField="ggjxh" HeaderText="规格及型号" ReadOnly="True" />
<asp:TemplateField HeaderText="价格">
<ItemTemplate>
<asp:TextBox ID="TeBjg" Width=60px runat="server" Text='<%# Eval("jg") %>'></asp:TextBox>
</ItemTemplate>
<ItemStyle Width="100px" HorizontalAlign="Center" />
</asp:TemplateField>
<asp:BoundField DataField="dw" HeaderText="单位" ReadOnly="True" >
<ItemStyle HorizontalAlign="Center" Width="80px" />
</asp:BoundField>
<asp:BoundField DataField="y" HeaderText="月" ReadOnly="True" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
</Columns>
<PagerTemplate>
<asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page" Enabled=" <%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">首页</asp:LinkButton>
<asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev" CommandName="Page" Enabled=" <%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">上一页</asp:LinkButton>
<asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page" Enabled=" <%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">下一页</asp:LinkButton>
<asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page" Enabled=" <%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">尾页</asp:LinkButton>
(第 <asp:Label ID="LabelCurrentPage" runat="server" Text=" <%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"> </asp:Label> 页,共 <asp:Label ID="LabelPageCount" runat="server" Text=" <%# ((GridView)Container.NamingContainer).PageCount %>"> </asp:Label> 页 <asp:Label ID="LabelTotalCount" runat="server" Text=' <%# (((GridView)Container.NamingContainer).DataSource is System.Collections.ICollection) ? string.Format(",{0} 条记录", ((System.Collections.ICollection)((GridView)Container.NamingContainer).DataSource).Count) : "" %>'> </asp:Label>)
</PagerTemplate>
<EmptyDataTemplate>
未找到相关数据
</EmptyDataTemplate>
</asp:GridView>后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
bingData(0);
}
} //绑定GridView
protected void bingData(int PageCount)
{
DataTable SHDDT = MyWeb.MyData.GetDBList("Jxzj_Sys_Price", Asrr, "Jxzj_Sys_PriceID", "1");
this.GVjg.DataSource = SHDDT;
this.GVjg.DataBind();
this.GVjg.PageIndex = PageCount;
}
//翻页事件
protected void GVjg_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
bingData(e.NewPageIndex);
} //批量修改GridView里的值
protected void ButEdit_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvr in GVjg.Rows)
{
string ID = GVjg.DataKeys[gvr.DataItemIndex].Value.ToString();
string jg = ((TextBox)gvr.FindControl("TeBjg")).Text.ToString().Trim();
string[,] Arr = new string[1, 3];
Arr[0, 0] = "jg"; Arr[0, 1] = jg; Arr[0, 2] = "1";
MyWeb.MyData.UpdateDB("Jxzj_Sys_Price", Arr, ID);
}
MyWeb.Msg.Show(this,"更新成功!", Request.UrlReferrer.ToString());
}
GridView绑定了N页的数据,GridView绑定的第一页可以批量修改,但只要是翻页后,修改就有报错提示:
索引超出范围。必须为非负值并小于集合大小。
参数名: index
异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。
行 113: string ID = GVjg.DataKeys[gvr.DataItemIndex].Value.ToString();
下面是代码:
<asp:GridView ID="GVjg" CellPadding="0" CellSpacing="1" runat="server"
AutoGenerateColumns="False" Width="100%"
AllowPaging="True" DataKeyNames="Jxzj_Sys_PriceID" CssClass="GridView"
GridLines="None" onpageindexchanging="GVjg_PageIndexChanging" PageSize="8">
<Columns>
<asp:BoundField DataField="xh" HeaderText="序号" ReadOnly="True" >
<ItemStyle HorizontalAlign="Center" Width="40px" />
</asp:BoundField>
<asp:BoundField DataField="dl" HeaderText="大类" ReadOnly="True" />
<asp:BoundField DataField="clmc" HeaderText="材料名称" ReadOnly="True" />
<asp:BoundField DataField="ggjxh" HeaderText="规格及型号" ReadOnly="True" />
<asp:TemplateField HeaderText="价格">
<ItemTemplate>
<asp:TextBox ID="TeBjg" Width=60px runat="server" Text='<%# Eval("jg") %>'></asp:TextBox>
</ItemTemplate>
<ItemStyle Width="100px" HorizontalAlign="Center" />
</asp:TemplateField>
<asp:BoundField DataField="dw" HeaderText="单位" ReadOnly="True" >
<ItemStyle HorizontalAlign="Center" Width="80px" />
</asp:BoundField>
<asp:BoundField DataField="y" HeaderText="月" ReadOnly="True" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
</Columns>
<PagerTemplate>
<asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page" Enabled=" <%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">首页</asp:LinkButton>
<asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev" CommandName="Page" Enabled=" <%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">上一页</asp:LinkButton>
<asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page" Enabled=" <%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">下一页</asp:LinkButton>
<asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page" Enabled=" <%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">尾页</asp:LinkButton>
(第 <asp:Label ID="LabelCurrentPage" runat="server" Text=" <%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"> </asp:Label> 页,共 <asp:Label ID="LabelPageCount" runat="server" Text=" <%# ((GridView)Container.NamingContainer).PageCount %>"> </asp:Label> 页 <asp:Label ID="LabelTotalCount" runat="server" Text=' <%# (((GridView)Container.NamingContainer).DataSource is System.Collections.ICollection) ? string.Format(",{0} 条记录", ((System.Collections.ICollection)((GridView)Container.NamingContainer).DataSource).Count) : "" %>'> </asp:Label>)
</PagerTemplate>
<EmptyDataTemplate>
未找到相关数据
</EmptyDataTemplate>
</asp:GridView>后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
bingData(0);
}
} //绑定GridView
protected void bingData(int PageCount)
{
DataTable SHDDT = MyWeb.MyData.GetDBList("Jxzj_Sys_Price", Asrr, "Jxzj_Sys_PriceID", "1");
this.GVjg.DataSource = SHDDT;
this.GVjg.DataBind();
this.GVjg.PageIndex = PageCount;
}
//翻页事件
protected void GVjg_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
bingData(e.NewPageIndex);
} //批量修改GridView里的值
protected void ButEdit_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvr in GVjg.Rows)
{
string ID = GVjg.DataKeys[gvr.DataItemIndex].Value.ToString();
string jg = ((TextBox)gvr.FindControl("TeBjg")).Text.ToString().Trim();
string[,] Arr = new string[1, 3];
Arr[0, 0] = "jg"; Arr[0, 1] = jg; Arr[0, 2] = "1";
MyWeb.MyData.UpdateDB("Jxzj_Sys_Price", Arr, ID);
}
MyWeb.Msg.Show(this,"更新成功!", Request.UrlReferrer.ToString());
}
protected void GVjg_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GVjg.PageIndex = e.NewPageIndex;
bingData(e.NewPageIndex);
}
{
string ID = GVjg.DataKeys[0].Value.ToString();
string jg = ((TextBox)GVjg.Rows[i].FindControl("TeBjg")).Text.ToString().Trim();
string[,] Arr = new string[1, 3];
Arr[0, 0] = "jg"; Arr[0, 1] = jg; Arr[0, 2] = "1";
MyWeb.MyData.UpdateDB("Jxzj_Sys_Price", Arr, ID);
}
GridView1.DataKeyNames=new string[]{"id"};
GridView1.DataBind();
在需要编辑修改的时候int id=int.Parse(GridView1.Datakeys[e.RowIndex].Value.ToString()); //取出id作为编辑修改的条件带入
我在MyWeb.MyData.UpdateDB("Jxzj_Sys_Price", Arr, ID)中ID是不string类型的,请问该怎么处理?
这行下断
可能的原因是在翻页以后,数据重新绑定了,而你又没有绑定主键,导致DataKeys为空。
DataTable SHDDT = MyWeb.MyData.GetDBList("Jxzj_Sys_Price", Asrr, "Jxzj_Sys_PriceID", "1");
this.GVjg.DataSource = SHDDT;
this.GVjg.DataBind();
this.GVjg.PageIndex = PageCount;
GridView1.DataKeyNames=new string[]{"主键列名"};
注意这里绑定的是主键列名!不是值!
你可以设置断点单步调试,这种错误报错比较直接
查看下gridview的datasource是否重新做了绑定 ,是否设定了datakeynames属性
protected void bingData(int PageCount)
{
DataTable SHDDT = MyWeb.MyData.GetDBList("Jxzj_Sys_Price", Asrr, "Jxzj_Sys_PriceID", "1");
this.GVjg.DataSource = SHDDT;
this.GVjg.DataBind();
this.GVjg.PageIndex = PageCount;
}
你的DataTable每次绑定都是第一页不是吗?
最后的"1"应该改为PageCount
DataItemIndex指的是数据源中的索引。
而DataKeys集合里面要取到值,需要的是在GridView当中的索引。需要用RowIndex,每一页的索引都是从0开始的,所以你第一页是对的,是因为第一页的数据源索引和GridView索引相同,第二页肯定会错啦,GridView的索引又从0开始了。
string ID = GVjg.DataKeys[gvr.DataKeyNames["id"]].Value.ToString();
弄下
这里出错。数组越界呢 GridView是每次都查询所有的记录.. 第二页的DataItemIndex并没有归0 而是跟着前面的再加在外面定义个 int index=0 里面改下就好string ID = GVjg.DataKeys[i].Value.ToString();
string jg = ((TextBox)gvr.FindControl("TeBjg")).Text.ToString().Trim();
string[,] Arr = new string[1, 3];
i++;
Arr[0, 0] = "jg"; Arr[0, 1] = jg; Arr[0, 2] = "1";
MyWeb.MyData.UpdateDB("Jxzj_Sys_Price", Arr, ID);
}
MyWeb.Msg.Show(this,"更新成功!", Request.UrlReferrer.ToString());
this.GVjg.DataSource = SHDDT;
this.datakeysname=new string[] {"xh"};
this.GVjg.DataBind();
//this.GVjg.PageIndex = PageCount;注释点不需要的还有绑定的时候不需要传递页做为参数进去的
你的分页事件里面直接 this.gvig.pageindex=e.newpageindex;
databind();
对butEdit事件这么改
protected void ButEdit_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvr in GVjg.Rows)
{
string ID =gvr.cells[0].text //放你的序号列的列号
string jg = ((TextBox)gvr.FindControl("TeBjg")).Text.ToString().Trim();
string[,] Arr = new string[1, 3];
Arr[0, 0] = "jg"; Arr[0, 1] = jg; Arr[0, 2] = "1";
MyWeb.MyData.UpdateDB("Jxzj_Sys_Price", Arr, ID);
}
MyWeb.Msg.Show(this,"更新成功!", Request.UrlReferrer.ToString());
}这样应该可以的
总子gridview每次点击分页的时候绑定的数据源应该要一样的 这样批量修改的时候就能正确获取 进,既然绑定的都是SHDDT 应该没必要加this.GVjg.PageIndex = PageCount