Winform中DataGridView实现全选和反选

来源:互联网 发布:广联达预算软件盗版 编辑:程序博客网 时间:2024/06/14 19:15

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;
                }
            }
        }


0 0
原创粉丝点击