指定的参数已超出有效值的范围。 参数名: index
异常详细信息: System.ArgumentOutOfRangeException: 指定的参数已超出有效值的范围。参数名: index
后台代码:
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
String strsql = "delete from tab_sb_equipment where 设备编号=" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "";
BaseClass1.execsql(strsql);
bindgrig();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
((LinkButton)(e.Row.Cells[7].Controls[0])).Attributes.Add("onclick", "return confirm('确定要删除吗?')"); //错 误 行
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection Conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\Aquaculture\\App_Data\\Database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
Conn.Open();
SqlCommand Comm = new SqlCommand();
Comm.Connection = Conn;
Comm.CommandText = "update tab_sb_equipment set 设备名称=@设备名称,购进数量=@购进数量,购进单价=@购进单价,购进总额=@购进总额,购进时间=@购进时间 where 设备编号=@设备编号";
//Comm.Parameters.AddWithValue("@设备编号", GridView1.DataKeys[e.RowIndex].Value.ToString());
Comm.Parameters.AddWithValue("@设备编号", ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);
Comm.Parameters.AddWithValue("@设备名称", ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text);
Comm.Parameters.AddWithValue("@购进数量", ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text);
Comm.Parameters.AddWithValue("@购进单价", ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text);
Comm.Parameters.AddWithValue("@购进总额", ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text);
Comm.Parameters.AddWithValue("@购进时间", ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text);
Comm.ExecuteNonQuery();
Conn.Close();
GridView1.EditIndex = -1;
Response.Redirect("Equipment.aspx");
}
前台:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" BackColor="White" BorderColor="#336666"
BorderStyle="Double" BorderWidth="3px" CellPadding="4" GridLines="Horizontal"
PageSize="8" Width="942px"
onpageindexchanging="GridView1_PageIndexChanging"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdatabound="GridView1_RowDataBound" onrowdeleting="GridView1_RowDeleting"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating">
<FooterStyle BackColor="White" ForeColor="#333333" />
<RowStyle BackColor="White" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="设备编号" HeaderText="设备编号" />
<asp:BoundField DataField="设备名称" HeaderText="设备名称" />
<asp:BoundField DataField="购进数量" DataFormatString="{0}台" HeaderText="购进数量" />
<asp:BoundField DataField="购进单价" DataFormatString="{0}元" HeaderText="购进单价" />
<asp:BoundField DataField="购进总额" DataFormatString="{0}元" HeaderText="购进总额" />
<asp:BoundField DataField="购进时间" HeaderText="购进时间" />
<asp:CommandField HeaderText="编辑" ShowEditButton="True" />
<asp:CommandField HeaderText="删除" ShowCancelButton="False"
ShowDeleteButton="True" />
</Columns>
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
</asp:GridView>
高手们,帮帮小妹吧,一开始GridView1_RowUpdating这里报错,后来改了改上面又报错了,急求高手指点 --------------------编程问答-------------------- GridView1数数看有没有8列(第1列是0),或者用FindControl试试 --------------------编程问答-------------------- 是8列,FindControl怎么试,我是新手,麻烦说的具体点,谢谢了 --------------------编程问答--------------------
((LinkButton)(e.Row.FindControl("ControlID"))).Attributes.Add("onclick", "return confirm('确定要删除吗?')");
试试 --------------------编程问答-------------------- Cells[7] 的下标是从0开始的,0就表示1,后面依次类推。 --------------------编程问答--------------------
这个可以++ --------------------编程问答-------------------- cell[7]问题。改为cell[6]吧!或者是Cell[7]不是LinkButton!
自己单步试试! --------------------编程问答--------------------
+++ --------------------编程问答--------------------
我这么做之后,又报 未将对象引用设置到对象的实例这个错 --------------------编程问答-------------------- index取的不对,不是7,改为6 --------------------编程问答--------------------
((LinkButton)(e.Row.FindControl("ControlID"))).Attributes.Add("onclick", "return confirm('确定要删除吗?')");中的ControlID你改成了gridview中的LinkButton的ID了吗?
补充:.NET技术 , ASP.NET