版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/guhaozhang/article/details/80872062
实现效果
DataGridView列头添加复选框
//声明静态类变量
private static DataGridViewCheckBoxColumn checkColumn = null;
private void cmbJiBie_SelectedIndexChanged(object sender, EventArgs e)
{
//实例化checkColumn
if (UI.管理员.frmoper.checkColumn == null)
{
UI.管理员.frmoper.checkColumn = new DataGridViewCheckBoxColumn();
//插入第0列
dataGridView1.Columns.Insert(0, checkColumn);
}
}
删除选中列
private void btnDelete_Click(object sender, EventArgs e)
{
DialogResult dr = MessageBox.Show("删除后不可恢复,确定要删除选中的用户吗?", "提示", MessageBoxButtons.OKCancel);
if (dr == DialogResult.OK)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells[0];
Boolean flag = Convert.ToBoolean(checkCell.Value);
if (flag == true)
{
Facade.UserFacade Facade = new Facade.UserFacade();
Entity.UserInfo UserInfo = new Entity.UserInfo();
UserInfo.UserID = this.dataGridView1.Rows[i].Cells[1].Value.ToString();
string a = this.dataGridView1.Rows[i].Cells[1].Value.ToString();
bool flag1 = Facade.DeleteUser(UserInfo);
if (flag1 != false)
{
MessageBox.Show(string.Format("用户{0}删除成功!",a));
}
else
{
MessageBox.Show("删除失败!");
}
}
}
}
}
全选and反选
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
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;
}
}