Winform中DataGridView实现全选和反选

摘自: https://wenku.baidu.com/view/494e7d10084c2e3f5727a5e9856a561252d32129.html

  • 全选
 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;
       }
    }
}
  • 反选
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    //判断当前行是否被选中
    if ((bool)dataGridView1.Rows[i].Cells[0].EditedFormattedValue == true)
        //设置每一行的选择框为未选中
        dataGridView1.Rows[i].Cells[0].Value = false;
    else
        //设置每一行的选择框为选中
        dataGridView1.Rows[i].Cells[0].Value = true;
}

posted on 2022-06-28 12:26  manber  阅读(1084)  评论(0)    收藏  举报

导航