关于GridView修改和删除的问题!看不出哪错了,有高手帮忙下吗

songkui100 2010-03-31 01:05:36

protected void gvBind()
{
string strCmd = "select * from news_users";
DataSet ds = da.ExceDS(strCmd);
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{


this.GridView1.PageIndex = e.NewPageIndex;

this.GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
gvBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
gvBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int IntUserID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
string user_id = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString();
string pwd = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();
string name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString();
string email = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString();
string create_time = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString();
int age =Convert.ToInt32( ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString());
string address = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text.ToString();
string sex = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[8].Controls[0])).Text.ToString();
SqlDataReader sdr = da.ExceRead("Update news_users set user_id='" + user_id + "',pwd='" + pwd + "',name='" + name + "',email='" + email + "',create_time='" + create_time + "',age='" + age + "',address='" + address + "',sex='" + sex + "' where id='" + IntUserID + "'");
GridView1.EditIndex = -1;
sdr.Close();
gvBind();


}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int IntUserID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
SqlDataReader sdr = da.ExceRead("Delete from news_users where id='" + IntUserID + "'");
sdr.Close();
gvBind();
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gvBind();

}

运行的时候就说索引超出范围。必须为非负值并小于集合大小参数名: index
异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。
参数名: index
我user_id是从1开始的,但是在运行的时候主键ID他这也出来个TEXTBOX能修改了!看不出哪错了
...全文
111 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhandjq 2010-03-31
  • 打赏
  • 举报
回复
SqlDataReader sdr = da.ExceRead("Delete from news_users where id='" + IntUserID + "'");
这句话有问题,你是执行的删除语句,怎么还能返回SqlDataReader呢,是你函数用错了!
amandag 2010-03-31
  • 打赏
  • 举报
回复
才学编程知道什么是规范吗
==
如果是VS2008
C:\Program Files\Microsoft Visual Studio 9.0\VC#\Specifications\2052\CSharp Language Specification.doc

请参考

代码不规范会导致程序的可读性差,维护困难
amandag 2010-03-31
  • 打赏
  • 举报
回复
先说大的问题
1. 使用拼接sql语句而不是参数

再说小问题
1. 命名不规范(GridView1、IntUserID、gvBind)
2. 查找控件的方式(GridView1.Rows[e.RowIndex].Cells[8].Controls[0]))
3. .Text.ToString() (已经Text了还要ToString()?)
songkui100 2010-03-31
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 amandag 的回复:]
引用 11 楼 songkui100 的回复:
引用 9 楼 amandag 的回复:
1. 是否设置了GridView的DataKeyNames属性为表的主键
2. 楼主这个程序写的太不规范,目不忍睹


我要写的规范就不会出错来这问了


程序规范和是否出错没有关系
[/Quote]才学编程知道什么是规范吗,你刚下生就会跑啊,你杂这么厉害,还以为你多厉害在这,说了2点也没看你哪个对了,你一边玩吧,瞧不起新人就别进来多那一句在这
amandag 2010-03-31
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 songkui100 的回复:]
引用 9 楼 amandag 的回复:
1. 是否设置了GridView的DataKeyNames属性为表的主键
2. 楼主这个程序写的太不规范,目不忍睹


我要写的规范就不会出错来这问了
[/Quote]

程序规范和是否出错没有关系
songkui100 2010-03-31
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 amandag 的回复:]
1. 是否设置了GridView的DataKeyNames属性为表的主键
2. 楼主这个程序写的太不规范,目不忍睹
[/Quote]

我要写的规范就不会出错来这问了
shenlong0125 2010-03-31
  • 打赏
  • 举报
回复
DataSet ds = da.ExceDS(strCmd);
this.GridView1.DataSource = ds.Table[0];
amandag 2010-03-31
  • 打赏
  • 举报
回复
1. 是否设置了GridView的DataKeyNames属性为表的主键
2. 楼主这个程序写的太不规范,目不忍睹
amandag 2010-03-31
  • 打赏
  • 举报
回复
this.GridView1.DataBind();
==
gvBind();
songkui100 2010-03-31
  • 打赏
  • 举报
回复
不是分页的问题!!
songkui100 2010-03-31
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hjw01592 的回复:]
你的user_id是第一列吗?
gridview的cell,row,column这些都是从0开始编号的。如果你的use_id是第一列,那么应该用cells[0]
[/Quote]
user_id是第2列 第一列是主键的ID
l_i_liu 2010-03-31
  • 打赏
  • 举报
回复
分页那里错了吧,继续调用上面的方法:this.GridView1.DataBind(); 改成
gvBind();
ouzui 2010-03-31
  • 打赏
  • 举报
回复
GridView1.Rows[e.RowIndex-1] 看看
hjw01592 2010-03-31
  • 打赏
  • 举报
回复
你的user_id是第一列吗?
gridview的cell,row,column这些都是从0开始编号的。如果你的use_id是第一列,那么应该用cells[0]
Jessezu 2010-03-31
  • 打赏
  • 举报
回复
GridView1.Rows[e.RowIndex].Cells[7]
行索引或单元格索引超出范围
调试看e.RowIndex
浮生若梦丶 2010-03-31
  • 打赏
  • 举报
回复

62,266

社区成员

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

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

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

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