GridView在做行编辑的时候和删除的时候出现索引超出范围。必须为非负值并小于集合大小。

ld_17941733 2010-05-07 08:49:49
如题
怎么办 着急啊 各位帮帮忙 我上代码
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
//--------------------------这个是在GridView上进行编辑事件处理
GridView1.EditIndex = e.NewEditIndex;
//bind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//--------------------------这个是在GridView上进行删除
GridView1.DataKeyNames = new string[] { "id" };
string aa = GridView1.DataKeys[e.RowIndex].Value.ToString();
string sql = "delete from stx_project_foreign where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
OracleCommand cmd = new OracleCommand(sql, DbConn.GetConn());
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
//bind();
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//--------------------------这个是在GridView上进行修改
string sql = "update stx_project_foreign set 题目='"+((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim() + "' where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
OracleCommand cmd = new OracleCommand(sql, DbConn.GetConn());
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();


GridView1.EditIndex = -1;


}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
//--------------------------这个是在GridView上进行取消操作
GridView1.EditIndex = -1;
// bind();
}
...全文
285 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangbo4235 2011-01-02
  • 打赏
  • 举报
回复
"GridView1.DataKeyNames = new string[] { "id" };

这个要在绑定之前执行的吧,你在要删除了才来定义key,太晚了"

这个就是我想说的,你应该在页面中<asp:GridView DataKey="id"> ,这样就ok了
happy664618843 2011-01-02
  • 打赏
  • 举报
回复
GridView1.DataKeyNames = new string[] { "ID" };//主键

GridView1.Rows[row.RowIndex].FindControl() as TextBox;
lisonghe 2011-01-02
  • 打赏
  • 举报
回复
jerry_k
的方法能行
liufenfen 2010-05-11
  • 打赏
  • 举报
回复
在绑定的时候,加上这一句GridView1.DataKeyNames = new string[] { "id" };
在GridView1.DataBind();之前
z195389542 2010-05-11
  • 打赏
  • 举报
回复
你看一下 e.rowindex.是多少就不行了
错误提示已经说明白了。gridview中没有那一行。
hztltgg 2010-05-11
  • 打赏
  • 举报
回复
GridView1.DataKeyNames = new string[] { "id" };

这个要在绑定之前执行的吧,你在要删除了才来定义key,太晚了
ld_17941733 2010-05-11
  • 打赏
  • 举报
回复
这个好用
ld_17941733 2010-05-11
  • 打赏
  • 举报
回复
this.GridView1.DataKeyNames = new string[] { "ID" };//主键
GridView1.DataBind();
string aa = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
string sql = "delete from stx_project_foreign where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
OracleCommand cmd = new OracleCommand(sql, DbConn.GetConn());
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
GridView1.DataBind();
jerry_k 2010-05-11
  • 打赏
  • 举报
回复
把GridView1.DataKeyNames = new string[] { "id" };
放在 Page_Load()函数中定义,试一下。
zhoulingsmile 2010-05-11
  • 打赏
  • 举报
回复
帮你顶。。。
ld_17941733 2010-05-07
  • 打赏
  • 举报
回复
还是不行 别拿 清清月儿 的七十二绝技混弄我 她写的是好 我就是照着她写的 但是我们绑定的方法不一样
我现在就问 为什么出现
"GridView在做行编辑的时候和删除的时候出现索引超出范围。必须为非负值并小于集合大小"
这样的错误 多谢各位了
x276912756 2010-05-07
  • 打赏
  • 举报
回复
帮顶!!!!!!!!!!!!!!!!!!!!!!
xnsypf 2010-05-07
  • 打赏
  • 举报
回复
不知道怎么样,多谢楼主
zbssoft 2010-05-07
  • 打赏
  • 举报
回复
全在这儿了



protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
bind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id;
id = Convert.ToInt32(this.GridView1.DataKeys[e.RowIndex].Value);
DataSet ds1 = new DataSet();
string st1 = "delete from news where newid=" + id + "";
bool inser = db.UpdataBySql(st1);
if (inser == true)
{
//((LinkButton)e.RowIndex.Cells[10].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:\"" + e.RowIndex.Cells[2].Text + "\"吗?')");

}
bind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;
bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox title;

TextBox kind;
TextBox href;
TextBox date;
TextBox time;
int id=Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
title = (TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0];
kind = (TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0];
href=(TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0];
date = (TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0];
time = (TextBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0];
string ti = title.Text.Trim();
string ki = kind.Text.Trim();
string hr = href.Text.Trim();
string da = date.Text.Trim();
string tim=time.Text.Trim();
DataSet ds2 = new DataSet();
string str2 = "update news set title='"+ti+"',kind='"+ki+"',href='"+hr+"',dates='"+da+"',times='"+tim+"' where newid="+id+"";
bool inser = db.UpdataBySql(str2);
if (inser == true)
{

}
else { }
GridView1.EditIndex = -1;
bind();
}
fuda_1985 2010-05-07
  • 打赏
  • 举报
回复
string aa =(强制转换成控件原类型) (GridView1.Items[e.RowIndex].findcontrl("存id的控件")).Text(或都value反正就是存值的那个属性。);
lorinzhang 2010-05-07
  • 打赏
  • 举报
回复
string aa = GridView1.DataKeys["id"].Value.ToString();
ld_17941733 2010-05-07
  • 打赏
  • 举报
回复
string aa = GridView1.DataKeys[e.RowIndex].Value.ToString();
请看这里 我测试在这里出现了 错误
ld_17941733 2010-05-07
  • 打赏
  • 举报
回复
在线等 谢谢

62,046

社区成员

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

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

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

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