日期:2014-05-18  浏览次数:20356 次

求助。。。.Net中GridView控件的问题、、、、、

其中的“详细信息”是用的<asp:HyperLinkField HeaderText="详细信息" NavigateUrl="~/jcDetail.aspx" Text="详细信息" DataNavigateUrlFields="jc_ID" DataNavigateUrlFormatString="jcDetail.aspx?jc_ID={0}" />
删除使用的:<asp:CommandField ShowDeleteButton="True" />
在后台的代码中有: protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {
  database my_db = new database();
  string sqlstr = "delete jiaocai where jc_ID = ' " + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value) + " ' ";

  my_db.DataCom(sqlstr);
  Response.Redirect("tsxx.aspx");

  }
  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
  ((LinkButton)(e.Row.Cells[6].Controls[0])).Attributes.Add("onclick", "return confirm('确定要删除吗?')");
  }
  }
但是在运行的时候总是报如下的错误:指定的参数已超出有效值的范围。
参数名: index 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.ArgumentOutOfRangeException: 指定的参数已超出有效值的范围。
参数名: index

源错误: 


行 121: if (e.Row.RowType == DataControlRowType.DataRow)
行 122: {
行 123: ((LinkButton)(e.Row.Cells[6].Controls[0])).Attributes.Add("onclick", "return confirm('确定要删除吗?')");
行 124: }
行 125: }
 
请问:这是什么错误呢?谢谢!!!!

------解决方案--------------------
确定有下标是6的列吗?
if (e.Row.RowIndex>=0)
//代码
试试

------解决方案--------------------
“e.Row.Cells[6].Controls[0]”

如果你学GridView时候没有学过使用FindControl来找到列(因为设计师会随时重构控件的排序的),那么尽快学会。

这个代码可能是一个asp.net程序员被辞退的理由之一。因为其代码破坏了最基本的可维护、可变通性。