当前位置:编程学习 > C#/ASP.NET >>

再发100分,解决一个GridView翻页问题,翻页后不能修改

十万万个火急~ 今天要要交付了,昨天发了一条,还是没解决,希望各位大侠帮我看看,代码写全一点。小弟在这感谢各位了!!分少了点,只能发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());
  } --------------------编程问答-------------------- 友情帮顶了 --------------------编程问答--------------------  
protected void GVjg_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {
  GVjg.PageIndex = e.NewPageIndex;
  bingData(e.NewPageIndex);
  } --------------------编程问答-------------------- 你最好用gridview自带的分页 --------------------编程问答-------------------- Grewview有没有绑定一个主键?

也就是在Grewview属性中加入DataKeyNames="主键字段名" --------------------编程问答--------------------
引用 2 楼 dd__dd 的回复:
protected void GVjg_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {
  GVjg.PageIndex = e.NewPageIndex;
  bingData(e.NewPageIndex);
  }


没用啊,我试了 --------------------编程问答-------------------- 帮顶。。。 --------------------编程问答--------------------   for (int i = 0; i < GVjg.Rows.Count; i++)
        {
            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);
        } --------------------编程问答--------------------
引用 4 楼 ly302 的回复:
Grewview有没有绑定一个主键?

也就是在Grewview属性中加入DataKeyNames="主键字段名"



有的,有主键 --------------------编程问答--------------------
引用 7 楼 spark_wu 的回复:
for (int i = 0; i < GVjg.Rows.Count; i++)
  {
  string ID = GVjg.DataKeys[0].Value.ToString();
  string jg = ((TextBox)GVjg.Rows[i].FindControl("TeBjg")).Text.ToString().Trim();
  string[,] Arr = ……


这个虽然没报错,但不能批量修改了。 --------------------编程问答-------------------- 绑定的时候把主键作为DataKeys也绑进去

GridView1.DataSource=...;
GridView1.DataKeyNames=new string[]{"id"};
GridView1.DataBind();


在需要编辑修改的时候

int id=int.Parse(GridView1.Datakeys[e.RowIndex].Value.ToString()); //取出id作为编辑修改的条件带入
--------------------编程问答--------------------
引用 9 楼 asus2323 的回复:
引用 7 楼 spark_wu 的回复:
for (int i = 0; i < GVjg.Rows.Count; i++)
  {
  string ID = GVjg.DataKeys[0].Value.ToString();
  string jg = ((TextBox)GVjg.Rows[i].FindControl("TeBjg")).Text.ToString().Trim(……



为什么不能修改。,,
调试一下啊。。 --------------------编程问答--------------------
引用 10 楼 zx75991 的回复:
绑定的时候把主键作为DataKeys也绑进去

C# code

GridView1.DataSource=...;
GridView1.DataKeyNames=new string[]{"id"};
GridView1.DataBind();



在需要编辑修改的时候

C# code

int id=int.Parse(GridView1.Datakeys[e.……


我在MyWeb.MyData.UpdateDB("Jxzj_Sys_Price", Arr, ID)中ID是不string类型的,请问该怎么处理? --------------------编程问答-------------------- 是需要的值没有取到,还是没有运行到 修改功能 的代码处啊 --------------------编程问答--------------------
引用 11 楼 spark_wu 的回复:
引用 9 楼 asus2323 的回复:
引用 7 楼 spark_wu 的回复:
for (int i = 0; i < GVjg.Rows.Count; i++)
  {
  string ID = GVjg.DataKeys[0].Value.ToString();
  string jg = ((TextBox)GVjg.Rows[i].FindControl("TeBjg"))……


能正常执行检查不出哪里出错了 --------------------编程问答--------------------
引用 13 楼 spark_wu 的回复:
是需要的值没有取到,还是没有运行到 修改功能 的代码处啊


能加我QQ吗? --------------------编程问答-------------------- MyWeb.MyData.UpdateDB("Jxzj_Sys_Price", Arr,   Convert.ToInt32(ID)) --------------------编程问答-------------------- 公司不让上QQ,屏蔽了 --------------------编程问答-------------------- F9 +  断点

看 gvr.DataItemIndex --------------------编程问答-------------------- 你用断点追一下,应该可以解决吧 --------------------编程问答-------------------- 你的查询语句里设置每页显示数量的时候是不是没有把 从第X条记录 到 第(X+每页显示的数量)条记录的 这个数量-1呢,不减1的话肯定要超界的 --------------------编程问答-------------------- 能上QQ的朋友加一下我的QQ吧182528124    实在搞不清楚了,搞定后马上结贴。。。 --------------------编程问答-------------------- 把这个页面的源码打包发我邮箱 554006164@163.com  我帮你看下。 --------------------编程问答-------------------- 你的分页方式不好的! --------------------编程问答-------------------- 楼主,断点一下吧。。这问题很难说。。。 --------------------编程问答-------------------- string ID = GVjg.DataKeys[gvr.DataItemIndex].Value.ToString();
这行下断
可能的原因是在翻页以后,数据重新绑定了,而你又没有绑定主键,导致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属性
--------------------编程问答-------------------- 最好的方式就是断点了。。看是哪出了问题 --------------------编程问答-------------------- for循环那个方式是OK的,不能更新,你可以调试一下,看是没执行到方法体,还是值传入有误,你的更新方式gvr.DataItemIndex在分页中,会找到第一页而报错,如果实在不行,建议用dataApdater来实现 --------------------编程问答-------------------- 先设个断点看看! --------------------编程问答-------------------- 建议断点! --------------------编程问答-------------------- 当你pagechanging事件时还需要重新绑定一次数据 --------------------编程问答-------------------- 在找找,问题应该不是大问题。细心很重要。 --------------------编程问答-------------------- 路过学习 ,和谐接分 , 友情帮顶~! --------------------编程问答-------------------- string ID = GVjg.DataKeys[gvr.RowIndex].Value.ToString(); --------------------编程问答--------------------  
    //绑定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;
    }


你的DataTable每次绑定都是第一页不是吗?
最后的"1"应该改为PageCount --------------------编程问答-------------------- 不能编辑了 还没有打完
DataItemIndex指的是数据源中的索引。
而DataKeys集合里面要取到值,需要的是在GridView当中的索引。需要用RowIndex,每一页的索引都是从0开始的,所以你第一页是对的,是因为第一页的数据源索引和GridView索引相同,第二页肯定会错啦,GridView的索引又从0开始了。 --------------------编程问答-------------------- 断点吧。。。。。。。。JF --------------------编程问答-------------------- 索引不对。应该是DataKeyNames,
string ID = GVjg.DataKeys[gvr.DataKeyNames["id"]].Value.ToString();
--------------------编程问答-------------------- 同上,断点,然后!this.IsPostBack
弄下 --------------------编程问答--------------------  0 0 那个调戏下   第二页时  string ID = GVjg.DataKeys[0].Value.ToString();   Id 是否是正确的? --------------------编程问答-------------------- string ID = GVjg.DataKeys[gvr.DataItemIndex].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 --------------------编程问答-------------------- --------------------编程问答--------------------
哇,这么多朋友来回答问题啊
好热闹哦,
看来问题解决了,
呵呵.
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,