高分请教批量修改的问题

dzswej 2008-09-26 11:39:44
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
string xjg=this.TextBox1.Text;
foreach (GridViewRow gr in GridView1.Rows)
{
CheckBox lb1 = (CheckBox)gr.FindControl("CheckBox1");
if (lb1.Checked=true)
{
strSql = "Update sc_gj set fy='" + xjg + "' where gjid=" + dtTable.Rows[0]["gjid"].ToString() + "";
shuju.ExcuteSql(strSql);
}

}


}
GridView1控件中有CheckBox,我想批量修改我选中的其fy的值 ,但结果是我点击按钮时,出现的是它把我所有的CheckBox全部都选中,修改了第一条数据
...全文
216 36 打赏 收藏 转发到动态 举报
写回复
用AI写文章
36 条回复
切换为时间正序
请发表友善的回复…
发表回复
yueyebohe 2008-10-07
  • 打赏
  • 举报
回复
mark
asia310 2008-10-04
  • 打赏
  • 举报
回复
急求:本人欲在傲游(Maxthon)浏览器的工具栏上安装一个自定义按钮,该按钮的功能是:当我按下该按钮时,启动d:\euro\hot.exe程序(该程序功能是:在当前网页中,搜索关键字,并将搜索结果及相关字段内容存入名为Asia2008.dbf数据库中,关闭当前网页)。请各位大侠指点做法,如有可能给出相关源程序,小生万分感谢!(Euro2000@126.com)
tianya275 2008-09-28
  • 打赏
  • 举报
回复
fds
shawn293 2008-09-28
  • 打赏
  • 举报
回复
haha
shawn292 2008-09-28
  • 打赏
  • 举报
回复
ding
world684 2008-09-28
  • 打赏
  • 举报
回复
haha
world684 2008-09-28
  • 打赏
  • 举报
回复
fds
world684 2008-09-28
  • 打赏
  • 举报
回复
fds
wuhen269 2008-09-28
  • 打赏
  • 举报
回复
fds
amandag 2008-09-26
  • 打赏
  • 举报
回复
补充一下,你的GridView1应该要设置 DataKeyNames属性为你表的主键,据我猜测就是gjid

也就是有
<asp:GridView id="GridView1" runat="server" DataKeyNames="gjid"
...
amandag 2008-09-26
  • 打赏
  • 举报
回复
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
string xjg=this.TextBox1.Text;
string whereClause = string.Empty;
CheckBox chk;
foreach (GridViewRow r in GridView1.Rows)
{
chk = (CheckBox)r.FindControl("CheckBox1");
if (chk != null)
{
if (chk.Checked)
{
if (whereClause == string.Empty)
whereClause += "'" + GridView1.DataKeys[r.RowIndex].Value.ToString() + "'";
else
whereClause += ",'" + GridView1.DataKeys[r.RowIndex].Value.ToString() + "'";
}
}
}
if (whereClause != string.Empty)
{
SqlConnection cn = new SqlConnection(@"server=.\SQLExpress;uid=sa;pwd=sa;database=pubs");
//假设你的gjid为数字类型字段
string strSQL = ""Update sc_gj set fy='" + xjg + "' where gjid in (" + whereClause + ")";
SqlCommand cmd = new SqlCommand(strSQL, cn);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
//下面重新绑定GridView
BindGrid();
}
dzswej 2008-09-26
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 amandag 的回复:]
一般是用,号拼接sql语句,最后执行一次即可
[/Quote]

请教具体怎么弄啊
amandag 2008-09-26
  • 打赏
  • 举报
回复
一般是用,号拼接sql语句,最后执行一次即可
jiang_jiajia10 2008-09-26
  • 打赏
  • 举报
回复
 for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (cbox.Checked == true)
{

strSql = "Update sc_gj set fy='" + xjg + "' where gjid=" + dtTable.Rows[i]["gjid"].ToString() + "";
shuju.ExcuteSql(strSql);
}
}


dzswej 2008-09-26
  • 打赏
  • 举报
回复
我把他改成这样也还是不行
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox lb1 = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (lb1.Checked== true)
{
strSql = "Update sc_gj set fy='" + xjg + "' where gjid=" + dtTable.Rows[i]["gjid"].ToString() + "";
shuju.ExcuteSql(strSql);
}

}
tautaulee 2008-09-26
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 dzswej 的回复:]
引用 6 楼 paulin 的回复:
上面说错了
strSql = "Update sc_gj set fy='" + xjg + "' where gjid=" + dtTable.Rows[0]["gjid"].ToString() + "";

看这句sql只会改变一次数据库啊
dtTable.Rows[i]["gjid"].ToString()


i怎么定义啊?
[/Quote]
你可以用for 不用foreach
calmer18 2008-09-26
  • 打赏
  • 举报
回复
if (lb1.Checked=true)

if (lb1.Checked==true)
dzswej 2008-09-26
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 paulin 的回复:]
上面说错了
strSql = "Update sc_gj set fy='" + xjg + "' where gjid=" + dtTable.Rows[0]["gjid"].ToString() + "";

看这句sql只会改变一次数据库啊
dtTable.Rows[i]["gjid"].ToString()
[/Quote]

i怎么定义啊?
tautaulee 2008-09-26
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 paulin 的回复:]
上面说错了
strSql = "Update sc_gj set fy='" + xjg + "' where gjid=" + dtTable.Rows[0]["gjid"].ToString() + "";

看这句sql只会改变一次数据库啊
dtTable.Rows[i]["gjid"].ToString()
[/Quote]
恩 对。顶。 但是最好别这样做。
paulin 2008-09-26
  • 打赏
  • 举报
回复
上面说错了
strSql = "Update sc_gj set fy='" + xjg + "' where gjid=" + dtTable.Rows[0]["gjid"].ToString() + "";

看这句sql只会改变一次数据库啊
dtTable.Rows[i]["gjid"].ToString()
加载更多回复(16)

62,046

社区成员

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

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

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

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