DataGridView的CheckBox列全选问题

nightmaple 2010-11-11 11:40:34
DataGridView的第一列为CheckBox(全选),第2,3,4,5列也为CheckBox,当某行的第一个CheckBox选上时,2,3,4,5也跟着选上。

界面上另有一个复选框,当选中复选框时,DataGridView的第一列ChechBox全选(相应的2,3,4,5由于第一列选中也会选中),当我不选中复选框时,DataGridView全不选(效果同选中时一样)。

当我点保存时,将以上所有改动一次保存。

我现在的问题是,当我在界面操作时,显示的都是能满足我的要求的;当我点保存时,第一行的改动被无视,其它行的修改正确地保存进了数据库。

要怎么修正,现有代码的问题是什么?
//以下是界面上复选框的事件代码
private void ckbAll_CheckedChanged(object sender, EventArgs e)
{
foreach (DataGridViewRow dr in this.dataGridView1.Rows)
{
if (dr.Cells["PAll"].Value.ToString() != ckbAll.Checked.ToString())
{
dr.Cells["PAll"].Value = ckbAll.Checked;
}
}
}

//以下是当第一列checkbox选中状态改变时,触发2,3,4,5列选中状态改变
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
//PAll列由选择状态变成未选状态
if (this.dataGridView1.Columns[e.ColumnIndex].Name == "PAll")
{
if (this.dataGridView1.Rows[e.RowIndex].Cells["PAll"].Value.ToString() == "False")
{
this.dataGridView1.Rows[e.RowIndex].Cells["PInsert"].Value = "False";
this.dataGridView1.Rows[e.RowIndex].Cells["PDelete"].Value = "False";
this.dataGridView1.Rows[e.RowIndex].Cells["PUpdate"].Value = "False";
this.dataGridView1.Rows[e.RowIndex].Cells["PSelect"].Value = "False";
}
else
{
this.dataGridView1.Rows[e.RowIndex].Cells["PInsert"].Value = "True";
this.dataGridView1.Rows[e.RowIndex].Cells["PDelete"].Value = "True";
this.dataGridView1.Rows[e.RowIndex].Cells["PUpdate"].Value = "True";
this.dataGridView1.Rows[e.RowIndex].Cells["PSelect"].Value = "True";
}
}
}
}

//批量更新到数据库的代码
public bool ExecuteBatchCommandBySQL()
{
OpenCon();//打开数据库连接
SqlDataAdapter sd = new SqlDataAdapter();
sd.SelectCommand = new SqlCommand("select * from PurSpecAnnPopedom", con);
SqlCommandBuilder scb = new SqlCommandBuilder(sd);
try
{
lock (this)
{
sd.Update(ds.Tables["Popedom"]);
}
}
catch (Exception ex)
{
throw ex;
}
return true;
}
...全文
1320 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
duenyu 2010-12-20
  • 打赏
  • 举报
回复
然后模拟点击事件
duenyu 2010-12-20
  • 打赏
  • 举报
回复
不用change事件,用click事件,然后判断checkbox状态,进行设置其他checkbox的状态,然后保存
zjx198934 2010-11-26
  • 打赏
  • 举报
回复

foreach (DataGridViewRow dr in this.dataGridView1.Rows)
{
if (dr.Cells["PAll"].Value.ToString() != ckbAll.Checked.ToString())
{
dr.Cells["PAll"].Value = ckbAll.Checked;
}
}
改成

for(int i = 0;i<this.dataGridView1.Rows.length,i++)
{
if(this.dataGridView1.Rows[i].Cells["PAll"].Value.ToString()!=ckbAll.Checked.ToString())
{
this.dataGridView1.Rows[i].Cells["PAll"].Value = ckbAll.Checked
}

nightmaple 2010-11-26
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 zjx198934 的回复:]
C# code

foreach (DataGridViewRow dr in this.dataGridView1.Rows)
{
if (dr.Cells["PAll"].Value.ToString() != ckbAll.Checked.ToString())
{
……
[/Quote]

还是一样的结果
nightmaple 2010-11-22
  • 打赏
  • 举报
回复
UPUPUP
nightmaple1 2010-11-18
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 whrspsoft3723 的回复:]
没有保存的那个是最后操作的吧?
[/Quote]

不是啊,就是点窗体上的一个复选框,触发事件使DataGridView中所以的复选框都变成跟窗体上复选框一样的状态(选中或不选中),然后点保存,将DataGridView中的新数据保存到数据库。

除了第一行的不保存存,其它行都保存到了数据库
whrspsoft3723 2010-11-18
  • 打赏
  • 举报
回复
没有保存的那个是最后操作的吧?
nightmaple1 2010-11-18
  • 打赏
  • 举报
回复
每日一顶,直到问题解决
nightmaple1 2010-11-18
  • 打赏
  • 举报
回复
楼上的图看得到吗?
nightmaple1 2010-11-18
  • 打赏
  • 举报
回复
nightmaple1 2010-11-18
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 zqd5920 的回复:]
怎么感觉有点像树结构阿
[/Quote]

不是树结构,只是很简单的一个功能,贴张图
平生我自如 2010-11-18
  • 打赏
  • 举报
回复
怎么感觉有点像树结构阿
nightmaple1 2010-11-18
  • 打赏
  • 举报
回复
UPUPUP
nightmaple1 2010-11-17
  • 打赏
  • 举报
回复
每日一顶,直到问题解决
nightmaple 2010-11-16
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 arthur080319 的回复:]
引用 6 楼 nightmaple 的回复:

刚试了下,在调批量保存方法前将焦点转移,但还是一样的结果。

我有调试,发现
dr.Index为0的时候,执行dr.Cells["PAll"].Value = ckbAll.Checked后,datatable中0行的行状态为Uncha……


试下在每次循环修改后执行一下 ds.Tables["Popedom"].AcceptCha……
[/Quote]

这样做所有修改的记录都没有保存
nightmaple 2010-11-15
  • 打赏
  • 举报
回复
UPUPUP
Arthur080319 2010-11-15
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 nightmaple 的回复:]

刚试了下,在调批量保存方法前将焦点转移,但还是一样的结果。

我有调试,发现
dr.Index为0的时候,执行dr.Cells["PAll"].Value = ckbAll.Checked后,datatable中0行的行状态为Uncha……
[/Quote]

试下在每次循环修改后执行一下 ds.Tables["Popedom"].AcceptChange()
nightmaple 2010-11-12
  • 打赏
  • 举报
回复
UPUPUP
guoyanhong1111 2010-11-11
  • 打赏
  • 举报
回复
1.先将网格控件提交:
this.网格控件名.EndEdit(DataGridViewDataErrorContexts.Commit);
this.网格控件名.CommitEdit(DataGridViewDataErrorContexts.Commit);
2.再用this.validate();
余胜国 2010-11-11
  • 打赏
  • 举报
回复
试试在保存前把焦点也就是focus定位到另外一个控件上,我也遇到过类似的问题。希望对你有帮助。
加载更多回复(7)

110,499

社区成员

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

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

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