在线等!如何操作checkbox里得到的值

dickzhang2 2010-08-23 03:55:16
for (int i = 0; i < exdgLog.Items.Count; i++)
{

CheckBox cb = exdgLog.Items[i].FindControl("cbxLogData ") as CheckBox;
if (cb.Checked == true)
{
string strSQLDelete = "DELETE FROM LOGINFO LOG_ID = '" + + "'";
DBHelper.ExecSQL(strSQLDelete);
}
}


我需要删除checkbox选中的值 。LOG_ID(日志表主键)应该等于什么呢?

foreach (DataGridItem i in exdgLog.Items)
{

if (((CheckBox)i.FindControl("cbxLogData")).Checked == true)
{
//记录选中的日志ID
string strDelSql = "DELETE FROM LOGINFO WHERE LOG_ID='" + i.Cells[1].Text + "'";
DBHelper.ExecSQL(strDelSql);
BindData();
}
}

用这种方法我可以得到checkbox的text 但是报错invalidOperationException(集合已修改;可能无法执行枚举操作)。


帮帮我 ! 解决尽量详细点 我得学会了。
...全文
175 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
yeyucao123 2010-08-23
  • 打赏
  • 举报
回复
LZ如果要用foreach也可以

string ids="";
foreach (DataGridItem i in exdgLog.Items)
{

if (((CheckBox)i.FindControl("cbxLogData")).Checked == true)
{
int count=0;
if(count==0)
{
ids=i.Cells[1].Text ;count++;
}esle{
ids+=","+ i.Cells[1].Text ;
}

}
}

public void method(string ids)
{
string [] id=ids.split(',');
for(int i=0;i<id.length;i++)
{
string strDelSql = "DELETE FROM LOGINFO WHERE LOG_ID='" + id[i]+ "'";
DBHelper.ExecSQL(strDelSql);
}
BindData();
}

wuyq11 2010-08-23
  • 打赏
  • 举报
回复
多线程环境下,其他线程修改了集合
另外一个线程对集合进行枚举操作,读取脏数据
for循环操作
itrefer 2010-08-23
  • 打赏
  • 举报
回复
这个用html的Checkbox最好使
不懂装懂 2010-08-23
  • 打赏
  • 举报
回复
for(int i=0;i<gv.rows.Count;i++)
{
string id=gv.rows[i].cell[1].tostring();
if(删除)
{
i--;
}
}
大概就这样,手打的,不对就自己找下吧
dickzhang2 2010-08-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 fdh120 的回复:]
for(int i=0;i<gv.rows.Count;i++)循环,当执行删除时i--
[/Quote]

用for 如何匹配表id是否就是checkbox选中的id
不懂装懂 2010-08-23
  • 打赏
  • 举报
回复
foreach (DataGridItem i in exdgLog.Items)
{

if (((CheckBox)i.FindControl("cbxLogData")).Checked == true)
{
//记录选中的日志ID
string strDelSql = "DELETE FROM LOGINFO WHERE LOG_ID='" + i.Cells[1].Text + "'";
DBHelper.ExecSQL(strDelSql);
}
}
BindData();......直接放外边
或者for(int i=0;i<gv.rows.Count;i++)循环,当执行删除时i--
dickzhang2 2010-08-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 lvsmile 的回复:]
foreach (DataGridItem i in exdgLog.Items)
你对集合exdgLog.Item进行操作了以后集合就被改变了,foreach无法进行这样的操作,改为for循环即可
[/Quote]

string strSQLDelete = "DELETE FROM LOGINFO LOG_ID = '" + + "'";

用for我不知道怎么把id和选中的id做匹配
dickzhang2 2010-08-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyazhe 的回复:]
不用foreach,改用for循环。
另外,你为什么不组合一个
delete from loginfo where log_id in (id1,id2,id3....)这样一个字符串一次性删掉所有选中的行?
[/Quote]

我现在是日志的id不知怎么弄成等于checkbox选中的那行的id
LVSmile 2010-08-23
  • 打赏
  • 举报
回复
foreach (DataGridItem i in exdgLog.Items)
你对集合exdgLog.Item进行操作了以后集合就被改变了,foreach无法进行这样的操作,改为for循环即可
dickzhang2 2010-08-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 djc7811 的回复:]
CheckBox.Checked =False
[/Quote]

如果等于false 那你的意思就是反选啦,选中的不删?
dickzhang2 2010-08-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 cc_net 的回复:]
foreach操作完以后在重新绑定数据
[/Quote]

帮了啊 ! 那个异常是在in上报的 ! 不明白了
dai78 2010-08-23
  • 打赏
  • 举报
回复
CheckBox.Checked =False
兔子-顾问 2010-08-23
  • 打赏
  • 举报
回复
不用foreach,改用for循环。
另外,你为什么不组合一个
delete from loginfo where log_id in (id1,id2,id3....)这样一个字符串一次性删掉所有选中的行?
cc_net 2010-08-23
  • 打赏
  • 举报
回复
foreach操作完以后在重新绑定数据

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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