当前位置:编程学习 > C#/ASP.NET >>

gridview 删除行时出现指定的参数已超出有效值的范围问题

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string sqlconstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
              SqlConnection sqlconn = new SqlConnection(sqlconstr);
              sqlconn.Open();
              //删除行处理
              String sql = "delete from consumer where name=@name" + ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);//删除语句
              SqlCommand Comm = new SqlCommand(sql, sqlconn);

             // Comm.Parameters.AddWithValue("@name",((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);

              Comm.ExecuteNonQuery();
              sqlconn.Close();
              sqlconn = null;
              Comm = null;
              GridView1.EditIndex = -1;
              bindgrid();
        }
总是显示:异常详细信息: System.ArgumentOutOfRangeException: 指定的参数已超出有效值的范围。
但 在编辑更新中相同代码((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text却没有错,并没有出现什么超出有效范围,这是什么情况?求大神帮忙!感谢! GridView --------------------编程问答-------------------- 求大神快速回复 --------------------编程问答-------------------- 这个要看你的界面控件的布局顺序,你写的Cells[0],对于删除按钮不一定就是0. --------------------编程问答-------------------- String sql = "delete from consumer where name=@name" + ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);//删除语句
删除
+ ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);//删除语句
--------------------编程问答--------------------
引用 2 楼 guwei4037 的回复:
这个要看你的界面控件的布局顺序,你写的Cells[0],对于删除按钮不一定就是0.

Cells[]这个应该是列吧,顺序下标都是都是从0开始的吧。
我的删除按钮就在编辑(编辑、更新、取消)按钮后。更新操作中也是这么写的都没有错 --------------------编程问答--------------------
引用 3 楼 flashposition 的回复:
String sql = "delete from consumer where name=@name" + ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);//删除语句
删除
+ ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);//删除语句

没看出区别啊??? --------------------编程问答--------------------
不明白 --------------------编程问答-------------------- @name 这里定义的变量。
// Comm.Parameters.AddWithValue("@name",((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);
这里又注释了,搞不懂你。 --------------------编程问答-------------------- 新手完全看不懂,
为什么我这边只有DataGridView?

如果他们功能差不多的话可以SelectedRows[]取指定列的值...

请大神解惑 --------------------编程问答--------------------
引用 2 楼 guwei4037 的回复:
这个要看你的界面控件的布局顺序,你写的Cells[0],对于删除按钮不一定就是0.

为什么要集体处理,而不是每个控件单独触发呢? --------------------编程问答--------------------
引用 8 楼 Gozs_cs_dn 的回复:
新手完全看不懂,
为什么我这边只有DataGridView?

如果他们功能差不多的话可以SelectedRows[]取指定列的值...

请大神解惑


你那是winform,他这是asp.net。
--------------------编程问答--------------------
引用 10 楼 guwei4037 的回复:
Quote: 引用 8 楼 Gozs_cs_dn 的回复:

新手完全看不懂,
为什么我这边只有DataGridView?

如果他们功能差不多的话可以SelectedRows[]取指定列的值...

请大神解惑


你那是winform,他这是asp.net。


谢谢哦 --------------------编程问答--------------------
引用 7 楼 guwei4037 的回复:
@name 这里定义的变量。
// Comm.Parameters.AddWithValue("@name",((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);
这里又注释了,搞不懂你。

 哦,忘了删除@name,
 String sql = "delete from consumer where name=" + GridView1.DataKeys[e.RowIndex].Value.ToString()+"";
这样,还是一样的错误。注释是因为下面这样写过
String sql = "delete from consumer where name=@name";
Comm.Parameters.AddWithValue("@name",((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);
还是一样的错误。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,