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

Skrr. 2019-05-07 12:33:42
请教下大佬帮忙看下,我这个改了很多次一直都是指定的参数已超出有效值的范围。。

点击编辑,更新列表出错。。

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
///方法一:需改进
// string sqlstr = "update Teacher set teacher_id='"
//+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',teacher_name='"
//+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',teacher_sex='"
//+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "',DepartmentName='"
//+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + "',Position='"
//+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim() + "',Telephone='"
//+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim() + "' where teacher_id='"
//+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
// mycom = new SqlCommand(sqlstr,myconn);
// mycom.ExecuteNonQuery();
// GridView1.EditIndex = -1;
// tech_show();
///方法二:
//当前更新的行数
int index = e.RowIndex;
//(重点)获取格子里面,第index行,第0号单元格里面第0个控件(也就是TextBox)强转,获取值
string newsteach_id = ((TextBox)this.GridView1.Rows[index].Cells[0].Controls[0]).Text.ToString();
string newsteach_name = ((TextBox)this.GridView1.Rows[index].Cells[1].Controls[0]).Text.ToString();
string newsteach_sex = ((TextBox)this.GridView1.Rows[index].Cells[2].Controls[0]).Text.ToString();
string newsteach_xibu = ((TextBox)this.GridView1.Rows[index].Cells[3].Controls[0]).Text.ToString();
string newsteach_zhicheng = ((TextBox)this.GridView1.Rows[index].Cells[4].Controls[0]).Text.ToString();
string newsteach_tel = ((TextBox)this.GridView1.Rows[index].Cells[5].Controls[0]).Text.ToString();
//这里的id是绑定上去的主键
int id = Convert.ToInt32(this.GridView1.DataKeys[index].Value);
////////////////////////
GridView1.EditIndex = -1;
myconn.Open();
string str = "select * from Teacher";
mydata = new SqlDataAdapter(str, myconn);
myset = new DataSet();
mydata.Fill(myset, "teacher");
DataTable dt = myset.Tables["teacher"];
dt.PrimaryKey = new DataColumn[] { dt.Columns["teacher_id"] };
DataRow dr = dt.Rows.Find(id);
dr["teacher_id"] = newsteach_id;
dr["teacher_name"] = newsteach_name;
dr["teacher_sex"] = newsteach_sex;
dr["DepartmentName"] = newsteach_xibu;
dr["Position"] = newsteach_zhicheng;
dr["Telephone"] = newsteach_tel;
SqlCommandBuilder mycb = new SqlCommandBuilder(mydata);
mydata.Update(myset.Tables["teacher"]);
myconn.Close();
tech_show();

}



...全文
364 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
kmbrkj 2019-05-13
  • 打赏
  • 举报
回复
.Controls用来读模板列列中的控件,如果不使用模版列,就没必要了
stherix 2019-05-07
  • 打赏
  • 举报
回复
Cells[x].Value.ToString就可以了 为什么要.Controls呢
  • 打赏
  • 举报
回复
我都是用自定义模板列的, 设置浏览和编辑 。

62,040

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧