我有一个带有自动生成的删除按钮的gridview。网格有一个数据键名,由于某种原因,我收到此错误:消息:索引超出范围。必须是非负数且小于集合的大小。参数名称:索引
我看过很多教程和例子。这段代码应该可以正常工作吗?
protected void grdBins_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int rec_id = int.Parse(grdBins.DataKeys[e.RowIndex].Value.ToString());
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "delete from t_run_schedule_lots " +
"where rec_id = @id";
cmd.Parameters.Add("@id", SqlDbType.Int).Value = rec_id ;
cmd.CommandType = CommandType.Text;
cmd.Connection = this.sqlConnection1;
this.sqlConnection1.Open();
//execute insert statement
cmd.ExecuteNonQuery();
this.sqlConnection1.Close();
//re-populate grid */
fill_grid();
grdBins.EditIndex = -1;
grdBins.DataBind();
// this bit was just to see if I was capturing the ID field properly.
lblBins.Visible = true;
lblBins.Text = rec_id.ToString();
}
如果有人知道 C# 中的一个很好的例子可以完成这项工作,那将不胜感激。