DataGridViewCheckBoxColumn dgCheck = new DataGridViewCheckBoxColumn();
初始加载
dgCheck.HeaderText = "全选";
dgCheck.ReadOnly = false;
dgCheck.TrueValue = true;
dgCheck.FalseValue = false;
dgCheck.FlatStyle = FlatStyle.Standard;
dgCheck.Name = "cb_check";
dataGridView1.Columns.Add(dgCheck);
dataGridView1.Columns.Contains(dgCheck);
dataGridView1.Columns.Remove(dgCheck);
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex == -1 && !headCkBoxIsChecked && e.ColumnIndex == 0)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
this.dataGridView1.Rows[i].Cells["cb_check"].Value = true;
}
headCkBoxIsChecked = true;
dgCheck.HeaderText = "已全选";
selectNum = dataGridView1.Rows.Count;
}
else if (e.RowIndex == -1 && headCkBoxIsChecked && e.ColumnIndex == 0)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
this.dataGridView1.Rows[i].Cells[0].Value = false;
}
headCkBoxIsChecked = false;
dgCheck.HeaderText = "全选";
selectNum = 0;
}
else if (e.ColumnIndex == 0)
{
if ((bool)dataGridView1.Rows[e.RowIndex].Cells["cb_check"].EditedFormattedValue == true)
{
this.dataGridView1.Rows[e.RowIndex].Cells["cb_check"].Value = false;
selectNum
serList.Remove((string)dataGridView1.Rows[e.RowIndex].Cells[2].Value);
}
else
{
this.dataGridView1.Rows[e.RowIndex].Cells["cb_check"].Value = true;
selectNum++;
serList.Add((string)dataGridView1.Rows[e.RowIndex].Cells[2].Value);
}
}
this.dataGridView1.RefreshEdit();
}