十万万个火急~ 今天要要交付了,昨天发了一条,还是没解决,希望各位大侠帮我看看,代码写全一点。小弟在这感谢各位了!!分少了点,只能发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>
  &nbsp;未找到相关数据&nbsp;
  </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());
  }

解决方案 »

  1.   

     
    protected void GVjg_PageIndexChanging(object sender, GridViewPageEventArgs e)
      {
      GVjg.PageIndex = e.NewPageIndex;
      bingData(e.NewPageIndex);
      }
      

  2.   

    Grewview有没有绑定一个主键?也就是在Grewview属性中加入DataKeyNames="主键字段名"
      

  3.   

      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.   

    绑定的时候把主键作为DataKeys也绑进去GridView1.DataSource=...;
    GridView1.DataKeyNames=new string[]{"id"};
    GridView1.DataBind();
    在需要编辑修改的时候int id=int.Parse(GridView1.Datakeys[e.RowIndex].Value.ToString()); //取出id作为编辑修改的条件带入
      

  5.   


    我在MyWeb.MyData.UpdateDB("Jxzj_Sys_Price", Arr, ID)中ID是不string类型的,请问该怎么处理?
      

  6.   

    MyWeb.MyData.UpdateDB("Jxzj_Sys_Price", Arr,   Convert.ToInt32(ID))
      

  7.   

    F9 +  断点看 gvr.DataItemIndex
      

  8.   

    能上QQ的朋友加一下我的QQ吧182528124    实在搞不清楚了,搞定后马上结贴
      

  9.   

    把这个页面的源码打包发我邮箱 [email protected]  我帮你看下。
      

  10.   

    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[]{"主键列名"};
    注意这里绑定的是主键列名!不是值!
      

  11.   

    肯定是数组越界的异常。
    你可以设置断点单步调试,这种错误报错比较直接
    查看下gridview的datasource是否重新做了绑定 ,是否设定了datakeynames属性
      

  12.   

    for循环那个方式是OK的,不能更新,你可以调试一下,看是没执行到方法体,还是值传入有误,你的更新方式gvr.DataItemIndex在分页中,会找到第一页而报错,如果实在不行,建议用dataApdater来实现
      

  13.   

    当你pagechanging事件时还需要重新绑定一次数据
      

  14.   

    string ID = GVjg.DataKeys[gvr.RowIndex].Value.ToString();
      

  15.   

         //绑定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
      

  16.   

    不能编辑了 还没有打完
    DataItemIndex指的是数据源中的索引。
    而DataKeys集合里面要取到值,需要的是在GridView当中的索引。需要用RowIndex,每一页的索引都是从0开始的,所以你第一页是对的,是因为第一页的数据源索引和GridView索引相同,第二页肯定会错啦,GridView的索引又从0开始了。
      

  17.   

    索引不对。应该是DataKeyNames,
    string ID = GVjg.DataKeys[gvr.DataKeyNames["id"]].Value.ToString();
      

  18.   

    同上,断点,然后!this.IsPostBack
    弄下
      

  19.   

     0 0 那个调戏下   第二页时  string ID = GVjg.DataKeys[0].Value.ToString();   Id 是否是正确的?
      

  20.   

    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());
      

  21.   

    绑定的时候加上
    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