在gridview更新是老是出现这个错误:指定的参数已超出有效值的范围。 参数名: index。拜托各位帮帮忙!
代码如下:protectedvoidGridView1_RowUpdating(objectsender,GridViewUpdateEventArgse){con.Co...
代码如下:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
con.ConDatabase();
string pr_id = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
string pr_name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
string ca_id = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
string pr_author = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();
string pr_price = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim();
string pr_mark = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim();
string sqlstr = "update product set pr_id='"
+ pr_id + "',pr_name='"+pr_name + "',ca_id='"+ ca_id + "' ,pr_author='"
+ pr_author + "',pr_price='"+ pr_price + "',pr_mark='"+ pr_mark+ "'where pr_id='"
+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
SqlCommand sqlcom = new SqlCommand(sqlstr, con.conn);
sqlcom.ExecuteNonQuery();
GridView1.EditIndex = -1;
Bind();
} 展开
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
con.ConDatabase();
string pr_id = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
string pr_name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
string ca_id = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
string pr_author = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();
string pr_price = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim();
string pr_mark = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim();
string sqlstr = "update product set pr_id='"
+ pr_id + "',pr_name='"+pr_name + "',ca_id='"+ ca_id + "' ,pr_author='"
+ pr_author + "',pr_price='"+ pr_price + "',pr_mark='"+ pr_mark+ "'where pr_id='"
+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
SqlCommand sqlcom = new SqlCommand(sqlstr, con.conn);
sqlcom.ExecuteNonQuery();
GridView1.EditIndex = -1;
Bind();
} 展开
展开全部
"指定的参数已超出有效值的范围"这个错误是指索引超出了数组最大范围,看看是不是你当前调用的索引值超出了你定义的数组的最大长度。
你上面的代码中,给gridview中的6个列绑定了字段值,如果你的gridview中一共只有6列的话,那么你调用的索引应该是从cell[0]至cell[5],而不是你上面写的从cell[1]至cell[6]。
你上面的代码中,给gridview中的6个列绑定了字段值,如果你的gridview中一共只有6列的话,那么你调用的索引应该是从cell[0]至cell[5],而不是你上面写的从cell[1]至cell[6]。
追问
谢谢,改过从cell[0]至cell[5],但还是这个错误。
追答
那你就用即时窗口,查看是不是你的cells里面查找controls[0],如果controls的count为0表示没有子控件,然后你再用即时窗口测试一下怎么找到你的子控件
展开全部
string pr_id = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim();
string pr_name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
string ca_id = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
string pr_author = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
string pr_price = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();
string pr_mark = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim();
gridview的列是从零开始的,你看看是不是超出有效值的范围
string pr_name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
string ca_id = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
string pr_author = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
string pr_price = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();
string pr_mark = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim();
gridview的列是从零开始的,你看看是不是超出有效值的范围
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询