当前位置: 代码迷 >> .NET Framework >> GridView 索引超出范围。必须为非负值并小于集合大小。参数名: index解决方法
  详细解决方案

GridView 索引超出范围。必须为非负值并小于集合大小。参数名: index解决方法

热度:108   发布时间:2016-05-02 00:45:57.0
GridView 索引超出范围。必须为非负值并小于集合大小。参数名: index
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="Vertical" OnSelectedIndexChanging="GridView1_SelectedIndexChanging" DataKeyNames="Rbbid">
  <Columns>
  <asp:BoundField DataField="rq" HeaderText="日期" />
  <asp:BoundField DataField="cm" HeaderText="船名" />
  <asp:BoundField DataField="ch" HeaderText="车号" />
  <asp:CommandField InsertVisible="False" ShowCancelButton="False" ShowSelectButton="True" />
  </Columns>
  <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
  <RowStyle BackColor="#EFF3FB" />
  <EditRowStyle BackColor="#2461BF" />
  <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
  <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
  <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
  <AlternatingRowStyle BackColor="White" />
  </asp:GridView>


  protected void Page_Load(object sender, EventArgs e)
  {
  Bind();
  }

  public void Bind()
  {
  SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
  con.Open();
  string sql = "select * from t_rbb";
  SqlDataAdapter da = new SqlDataAdapter(sql, con);
  DataSet ds = new DataSet();
  da.Fill(ds);
  GridView1.DataSource = ds;
  GridView1.DataBind();
  con.Close();
  } 
  protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
  {
  string Rbbid = this.GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
  Response.Redirect("rbbmx.aspx?Rbbid=" + Rbbid + "");
  }

异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。
参数名: index  

这是怎么回事呀。。。大家能告诉我吗?

------解决方案--------------------
调试一下,看是哪句出了问题
------解决方案--------------------
C# code
  protected void Page_Load(object sender, EventArgs e)  {   if(!IsPostBack)  Bind();  }  public void Bind()  {  SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);  con.Open();  string sql = "select * from t_rbb";  SqlDataAdapter da = new SqlDataAdapter(sql, con);  DataSet ds = new DataSet();  da.Fill(ds);  GridView1.DataSource = ds;   GridView1.DataKeyNames=new string[]{"id"}; //设置主键  GridView1.DataBind();  con.Close();  }    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)  {  string Rbbid = this.GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();  Response.Redirect("rbbmx.aspx?Rbbid=" + Rbbid + "");  }
------解决方案--------------------
加个判断
if(index>-1)
因为初始化的时候也会执行这个函数GridView1_SelectedIndexChanging
但这时候index是-1,
所有出错
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
if(NewSelectedIndex>-1)
string Rbbid = this.GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
Response.Redirect("rbbmx.aspx?Rbbid=" + Rbbid + "");
}
  相关解决方案