dataGridView1中添加checkbox列,实现全选和反选:
//全选
private void CheckAll_CheckedChanged(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count>0)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dataGridView1.Rows[i].Cells[0].Value = true;
}
}
}
//反选
private void CheckReverse_CheckedChanged(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count>0)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if ((bool)dataGridView1.Rows[i].Cells[0].EditedFormattedValue)
{
dataGridView1.Rows[i].Cells[0].Value = false;
}
else
dataGridView1.Rows[i].Cells[0].Value = true;
}
}
}
本文介绍了在Winform应用中,如何在DataGridView控件中实现全选和反选功能。通过监听CheckBox列的事件,遍历并设置行的选中状态来达到全选或反选的效果。
3039

被折叠的 条评论
为什么被折叠?



