gridview 更新 指定的参数已超出有效值的范围

来源:百度知道 编辑:UC知道 时间:2024/06/02 00:11:16
更新的代码如下 datakeynames属性已经设置
load事件 代码如下

if (Page.IsPostBack)
{
return;
}

更新的代码如下

string getid = this.GridView1.DataKeys[e.RowIndex][0].ToString();

string name = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString();
string money = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString();
SqlConnection con = new SqlConnection("server=.;uid=sa;database=mydatabase;pwd=");

con.Open();
SqlCommand cmd = new SqlCommand("update boss set name='" + name + "',money='" + money + "' where id=" + getid + "", con);
cmd.ExecuteNonQuery();
bd();
this.GridView1.EditIndex = -1;
con.Close();

进入编辑状态 和取消编辑状态 的代码都没有出错
运行的时候也不报错 但是一点更新按钮的时候就报错说

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

this.GridView1.EditIndex = -1
这句是写在取消事件里的啊

string getid = this.GridView1.DataKeys[e.RowIndex][0].ToString();
[0]改为VALUE

string name = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString();
string money = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString();

2句的Cells[2].Controls[0] 改
((TextBox)this.GridView1.Rows[e.RowIndex].FindControl("txtstuAge")).Text;
txtstuAge是你GridView1里模板列里编辑项textbox的ID