日期:2014-05-16  浏览次数:20487 次

【求大神解答】索引超出范围。必须为非负值并小于集合大小。参数名: index解决方案
直接上问题
  protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string strConn = System.Configuration.ConfigurationManager.AppSettings["DSN"];
        SqlConnection conn = new SqlConnection(strConn);
        conn.Open();

        string str1=GridView1.DataKeys[e.RowIndex].Value.ToString();
        SqlCommand cmd = new SqlCommand("delete from 户型资料表 where 户型编号='" +str1+ "'", conn);
        cmd.ExecuteNonQuery(); 
        conn.Close();
        GridView1.DataBind();
    
    }
string 那句出现索引问题。

 <asp:GridView ID="GridView1"  runat="server" Height="217px" Width="651px" AllowPaging="True" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" EnableSortingAndPagingCallbacks="True" PageSize="5" AllowSorting="True" AutoGenerateColumns="False" ForeColor="Black" GridLines="Vertical" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating1" >

这是表源。

我尝试过在这里加 datakeysnames="id";

然后去到protected void Page_Load(object sender, EventArgs e)
    {

       data();
       data1();
        
    }
    protected void data()
    {


        string strConn = System.Configuration.ConfigurationManager.AppSettings["DSN"];
        SqlConnection conn = new SqlConnection(strConn);
        conn.Open();
        SqlDataAdapter da = new SqlDataAdapter("select * from 户型资料表 ", conn);
        DataSet ds = new DataSet();
        da.Fill(ds, "户型资料表");
        GridView1.DataSource = ds;
        GridView1.DataBind();
    
    }

最后databind(),说不存在id输出型
------解决方案--------------------
datakeysnames="户型编号";