读书人

GridView指定的参数已超出有效值的范围

发布时间: 2012-04-10 21:03:56 作者: rapoo

GridView指定的参数已超出有效值的范围
GridView在更新过程中
异常详细信息: System.ArgumentOutOfRangeException: 指定的参数已超出有效值的范围。
参数名: index


本段代码如下

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

string stuID = GridView1.DataKeys[e.RowIndex].Value.ToString();

TextBox tb1 = (TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0];
string g1 = tb1.Text;// 显示错误在这两行上

string g2 = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
string g3 = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
string g4 = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
string updateSql = String.Format("update studentInfo set grade1='{0}',grade2='{1}',grade3='{2}',grade4='{3}' where stuID='{4}'",g1,g2,g3,g4,stuID);

SqlConnection conn = new SqlConnection("server=.;database=Elect;Integrated Security=SSPI");
conn.Open();
SqlCommand cmd = new SqlCommand(updateSql, conn);
int count=cmd.ExecuteNonQuery();
conn.Close();
if (count > 0)
{
Response.Write("<script>alert('更新成功!')</script>");
}
GridView1.EditIndex = -1;
BindGrid();
}

另:GridView总共是7列13排



[解决办法]
TextBox tb1 = (TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0];
看看取的控件对不对?
[解决办法]

探讨
TextBox tb1 = (TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0];
看看取的控件对不对?

[解决办法]
GridView1.Rows[e.RowIndex].Cells[2].Controls[0]
先确认是否有controls,在确定controls[0]是否是textbox
[解决办法]
探讨

试过了在后面加.ToString()转换格式,也还显示同样的错误

读书人网 >asp.net

热点推荐