在GridView添加CheckBox做删除

songkui100 2010-05-13 12:54:39
SQL是news
字段category_id int
news_id bigint (主键)
title nvarchar(max)
我在在在GridView添加了个checkbox后在点删除的时候就报错了
索引超出范围。必须为非负值并小于集合大小。
参数名: index
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。
参数名: index

源错误:


行 272: CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
行 273: // if(cbox.Checked==true){
行 274: string sql = "delete from news where news_id='" + GridView1.DataKeys[i].Value + "'";
行 275: da.ExceSQL(sql);
行 276: da.connectClose();

代码是 protected void Button7_Click(object sender, EventArgs e)
{
for (int i = 0; i <= GridView1.Rows.Count-1;i++ )
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if(cbox.Checked==true){
string sql = "delete from news where title='" + GridView1.DataKeys[i].Value + "'";
da.ExceSQL(sql);
da.connectClose();

}

}


}
也看不出哪快错了!!希望有高手帮忙下
...全文
187 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
bbb332 2010-07-25
  • 打赏
  • 举报
回复
或者是
//假设你在第一列绑定了 news_id
string sql = "delete from news where news_id=" + GridView1.Rows[i].Cells[0].Text;
bbb332 2010-07-25
  • 打赏
  • 举报
回复
string sql = "delete from news where news_id='" + GridView1.DataKeys[i].Value + "'";
改为
string sql = "delete from news where news_id=" + GridView1.DataKeys[i].Value;
因为是int,所以不用引号。。。
wwfgu00ing 2010-07-25
  • 打赏
  • 举报
回复
GridView1.DataKeyNames = new string[] { "ID" };
liuchengit 2010-07-25
  • 打赏
  • 举报
回复
我也遇到这个问题了。。还没有解决,for (int i = 0; i <= GridView1.Rows.Count-1;i++ )
{

if(cbox.Checked==true){
string sql = "delete from news where title='" + GridView1.DataKeys[i].Value + "'";
da.ExceSQL(sql);
da.connectClose();
}
不过报错的地方很明确
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
当i=0时没有问题,就是当i>0时出错,跟踪发现以后的行中的控件ID都自动增加了。。
不知道别人有没有用到。。
我的QQ:47307116
songkui100 2010-05-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 q107770540 的回复:]
C# code

//假设你在第一列绑定了 news_id
string sql = "delete from news where news_id='" + GridView1.Rows[i].Cells[0].Text+ "'";
[/Quote]
还是不对我在gridview中title是第一列这下写完没反应,没错误但是不好用
mngzilin 2010-05-13
  • 打赏
  • 举报
回复
GridView1.DataKeyNames = new string[] { "ID" };
mngzilin 2010-05-13
  • 打赏
  • 举报
回复
GridView1.DataKeysFiled=new string[]{"字段"}
希姐她爸 2010-05-13
  • 打赏
  • 举报
回复
 public void aa(object sender, System.EventArgs e)
{
CheckBox cb = (CheckBox)sender;

if (cb.Text == "全选")
{
foreach (GridViewRow gv in this.GridView1.Rows)
{
CheckBox cd = (CheckBox)gv.FindControl("CheckBox1");
cd.Checked = cb.Checked;
cb.Text = "反选";
}
}
else
{
cb.Text = "反选";
foreach (GridViewRow gv in this.GridView1.Rows)
{
CheckBox cd = (CheckBox)gv.FindControl("CheckBox1");
cd.Checked = cb.Checked;
cb.Text = "全选";
}

}
songkui100 2010-05-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wuyq11 的回复:]
foreach (GridViewRow row in GridView1.Rows)
{
cbx =(CheckBox)row.Cells[8].FindControl("cbxId");
if (cbx.Checked)
{
ids += ((HiddenField)(row.FindControl("HiddenField1"))).Value + "……
[/Quote]
我说大哥,就一个删除的功能。。。你做什么呢
q107770540 2010-05-13
  • 打赏
  • 举报
回复

//假设你在第一列绑定了 news_id
string sql = "delete from news where news_id='" + GridView1.Rows[i].Cells[0].Text+ "'";
wuyq11 2010-05-13
  • 打赏
  • 举报
回复
foreach (GridViewRow row in GridView1.Rows)
{
cbx =(CheckBox)row.Cells[8].FindControl("cbxId");
if (cbx.Checked)
{
ids += ((HiddenField)(row.FindControl("HiddenField1"))).Value + ",";
}
}
if(ids.EndWiths(",")
ids = ids.Substring(0, ids.Length - 1);

if(!IsPostBack)绑定数据

foreach (GridViewRow gr in this.Gridview1.Rows)
{
if (((CheckBox)gr.FindControl("CheckBox1")).Checked)
{}
}
imfor 2010-05-13
  • 打赏
  • 举报
回复
GridView1.Rows[i][0].Text?可以不
imfor 2010-05-13
  • 打赏
  • 举报
回复
你有给gridview绑定主键吗,还是要获取某一列的值?

62,074

社区成员

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

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

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

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