c# winform datagirdView Checkbox选中问题

seaman5126 2010-11-22 09:58:57
在C#中,我想让某个checkbox被选中,代码如下:datagridView1.Rows[i].cells[0].Value=true;

可是这样无法实现。在网上看有这种方法:
DataGridViewCheckBoxCell dataCheck = (DataGridViewCheckBoxCell)datagridView1.Rows[i].Cells[0];
dataCheck.Value = true;

可是也无法实现,请问是什么原因啊?
...全文
173 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
huwen7565833 2010-11-22
  • 打赏
  • 举报
回复
 //全选
private void btnSelectAll_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
((DataGridViewCheckBoxCell)dr.Cells[0]).Value = true;
}
}

//全不选
private void btnNoSelect_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
((DataGridViewCheckBoxCell)dr.Cells[0]).Value = false;
}
}
herott632482577 2010-11-22
  • 打赏
  • 举报
回复
private void button2_Click(object sender, EventArgs e)//全选/第1行 {
for (int i = 0; i < dataGridView2.Rows.Count; i++)
{
if ("False" == dataGridView2.Rows[i].Cells[0].FormattedValue.ToString())
{
dataGridView2.Rows[i].Cells[0].Value = true;
}
else if ("True" == dataGridView2.Rows[i].Cells[0].FormattedValue.ToString())
{
dataGridView2.Rows[i].Cells[0].Value = false;
dataGridView2.Rows[0].Cells[0].Value = true;
this.dataGridView2.VirtualMode = false;

}
}

}

DataGridViewCheckBoxColumn newColumn = new DataGridViewCheckBoxColumn();
newColumn.HeaderText = "选择";
dataGridView2.Columns.Insert(0, newColumn);
dataGridView2.MultiSelect = true;
newColumn.InheritedStyle.Alignment = DataGridViewContentAlignment.TopCenter;
newColumn.Width = 35;
this.dataGridView2.VirtualMode = false;
//第1列添加CHECKBOX
peterb 2010-11-22
  • 打赏
  • 举报
回复
那一列是DataGridViewCheckBoxColumn吗?
Qcontriver 2010-11-22
  • 打赏
  • 举报
回复
直接把那个CheckBox的checked属性设为True不就行了吗。。。
Checked="True"
gjw_8888 2010-11-22
  • 打赏
  • 举报
回复
check的值需要自定义true=?,false=?
定义好了赋值会表现为选中或者不选中
value并不等于true or false
air123456789 2010-11-22
  • 打赏
  • 举报
回复
不过 应该是datagridView1.Rows[i].cells[0].checked=true;吧?
air123456789 2010-11-22
  • 打赏
  • 举报
回复
看不懂, 没见过第二种那方法写的。
seaman5126 2010-11-22
  • 打赏
  • 举报
回复
datagridView1.Rows[i].cells[0].Value=true;
((DataGridViewCheckBoxCell)datagridView1.Rows[i].Cells[0]).Value = true;
以上都是正解,只是刚开始的时候我把它们放到构造函数中了,所以没法实现。
后来我把它们放到Load方法中就可以了,具体原理不明白,有知道的还不吝赐教!

111,129

社区成员

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

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

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