dataGridView 控制行是否全选

首先需要有一列为DataGridViewDisableCheckBox,然后再标题加一个checkbox,以此控制全选与否。

//绑定数据源后遍历dataGridView 确定是否显示checkbox

foreach (DataGridViewRow dgvr in dataGridView1.Rows)
{
if (dgvr.Cells["Column16"].Value.ToString() != "已导入待执行")
{
(dgvr.Cells["Column21"] as DataGridViewDisableCheckBoxCell).Display = false;
}

if (dgvr.Cells["Column15"].Value.ToString() == "" || dgvr.Cells["Column15"].Value.ToString() == "无")
{
(dgvr.Cells["Column22"] as DataGridViewDisableCheckBoxCell).Display = false;
}
}

注:点击列头重新排序后也需要遍历。

//判断是否显示checkbox

if (checkBox1.Checked == true)
{
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
if (dr.Cells["Column16"].Value.ToString() == "已导入待执行")
{
dr.Cells["Column21"].Value = true;
}
}
}
if (checkBox1.Checked == false)
{
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
if (dr.Cells["Column16"].Value.ToString() == "已导入待执行")
{
dr.Cells["Column21"].Value = false;
}
}
}

原文地址:https://www.cnblogs.com/xiguanjiandan/p/2738288.html