关于DataGridView的checkbox是否选中的问题

jimmy893 2009-04-30 10:37:25
winform程序
我的DataGridView第一列是checkbox,
我想点击一个按钮,得到哪些行的checkbox被选中了
...全文
122 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
jimmy893 2009-05-05
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 LoveLife_Go 的回复:]
C# codefor (int i = 0; i < dataGridView3.Rows.Count; i++)
{
if (Convert.ToBoolean(dataGridView3.Rows[i].Cells[0].FormattedValue) == true)
{

}
}
[/Quote]

FormattedValue-->EditedFormattedValue
蓝海D鱼 2009-04-30
  • 打赏
  • 举报
回复
click事件里面
//假设第一个列为选择列

if(this.dataGridView1.Rows[e.RowIndex].Cells[0].EditedFormattedValue.ToString()= = "True")
{
//
}

下面是设置CheckBox是选中(1)还是没有选中(null):

private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
{
dataGridView2.CommitEdit(DataGridViewDataErrorContexts.Commit);

if (e.ColumnIndex == 1)
{
if (((DataGridViewCheckBoxCell)dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex]).Tag == null)
((DataGridViewCheckBoxCell)dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex]).Tag = "1";
else
{
((DataGridViewCheckBoxCell)dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex]).Tag = "0";
}
}
}



下面是遍历CheckBox:

for (int j = 0; j < dataGridView2.Rows[i].Cells.Count; j++)
{
//判断是否是checkbox列
if (this.dataGridView2.Columns[j].GetType().Name == "DataGridViewCheckBoxColumn")
{
if ((((DataGridViewCheckBoxCell)dataGridView2.Rows[i].Cells[j]).Tag == null) || (((DataGridViewCheckBoxCell)dataGridView2.Rows[i].Cells[j]).Tag.ToString() == "0"))
{
strTmp1 = "";
}
else if (((DataGridViewCheckBoxCell)dataGridView2.Rows[i].Cells[j]).Tag.ToString() == "1")
{
ckb = dataGridView2.Rows[i].Cells[j].Value.ToString();
strTmp1 = strTmp1 + "," + ckb;
}
break;
}
}


LoveLife_Go 2009-04-30
  • 打赏
  • 举报
回复
for (int i = 0; i < dataGridView3.Rows.Count; i++)
{
if (Convert.ToBoolean(dataGridView3.Rows[i].Cells[0].FormattedValue) == true)
{

}
}
uncleson88 2009-04-30
  • 打赏
  • 举报
回复
判断 datagridview1[0,rowindex].value == null ;

111,126

社区成员

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

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

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