2014-10-22 60 views
0
protected void grd_RowDeleting(object sender, GridViewDeleteEventArgs e) 
    { 
     SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString()); 
     con.Open(); 
     string query = "delete from details where sno='"+grd.DataKeys[e.RowIndex].Value+"'";-----(getting error like Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index) 
     SqlCommand cmd = new SqlCommand(query,con); 
     cmd.ExecuteNonQuery(); 
     filldata(); 
     con.Close(); 
    } 

像索引gettnig錯誤超出範圍。必須大於收集在網格視圖行刪除

+0

你的問題是什麼? – Izzy 2014-10-22 10:35:26

回答

1

{

訪問
 SqlConnection con=newSqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString()); 
     con.Open(); 
     int Sno = Convert.ToInt32(grd.DataKeys[e.RowIndex].Values[0].ToString()); 
     string query="delete from Details where Sno="+Sno; 
     SqlCommand cmd = new SqlCommand(query, con); 
     int result = cmd.ExecuteNonQuery(); 
     con.Close(); 

}

+0

我不得不使用Grid1.Rows [Grid1.SelectedIndex] .Cells [1] .Text來讓它爲我工作。但是你的帖子確實指出了我的正確方向 – nate 2014-12-10 17:43:25

0

該錯誤是從下面的語句

grd.DataKeys[e.RowIndex] 

到來,意味着e.RowIndex值爲元素的範圍之外grd.DataKeys陣列中的大小非負和少。

要進一步調試,您需要找出e.RowIndex的值,並查看grd.DataKeys陣列中有多少元素。

0

你應該在grd_RowDeleting方法在.aspx頁面中添加DataKeyNames="your_id_column"屬性的GridView

然後你就可以grd.DataKeys[e.RowIndex].Valuegrd.DataKeys[e.RowIndex].Values["your_id_column"]

而且它更好地剖析結果字符串