DataGridView里CheckBox实现全选控制

时间:2022-03-07 04:04:44

1、 checkbox点击事件

DataGridView里CheckBox实现全选控制DataGridView里CheckBox实现全选控制
    private void myStyleDataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if ((e.ColumnIndex >= 0) && ((this.myStyleDataGridView1.Columns[e.ColumnIndex].Name.ToString().ToUpper() == "IsSelect".ToUpper()) && (e.RowIndex >= 0)) )
{
if (this.myStyleDataGridView1[e.ColumnIndex, e.RowIndex].Value == null)
{
this.myStyleDataGridView1.Rows[e.RowIndex].Cells["IsSelect"].Value = false;
}
if (this.myStyleDataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString().ToUpper() == "TRUE".ToUpper())
{
this.myStyleDataGridView1[e.ColumnIndex, e.RowIndex].Value = false;
}
else
{
this.myStyleDataGridView1[e.ColumnIndex, e.RowIndex].Value = true;
}

}
getTotal();

}
View Code

 

2、统计选中的行数

DataGridView里CheckBox实现全选控制DataGridView里CheckBox实现全选控制
/// <summary>
/// 统计
/// </summary>
private void getTotal()
{
int j = 0;
strCol003
= "";
for (int i = 0;i<this.myStyleDataGridView1.RowCount;i++)
{

if ((myStyleDataGridView1.Rows[i].Cells["IsSelect"].Value+"").ToUpper() == "TRUE")
{
j
++;
strCol003
+= "'" + myStyleDataGridView1.Rows[i].Cells["col_003"].Value+"" + "',";
}
}
strCol003
= strCol003.TrimEnd(',');
}
View Code

 

 

 

3、全选

DataGridView里CheckBox实现全选控制DataGridView里CheckBox实现全选控制
  private void ckAll_Click(object sender, EventArgs e)
{
for (int count = 0; count < this.myStyleDataGridView1.Rows.Count; count++)
{
DataGridViewCheckBoxCell cbh
= (DataGridViewCheckBoxCell)this.myStyleDataGridView1.Rows[count].Cells["IsSelect"];
if (ckAll.Checked)
{
this.myStyleDataGridView1.Rows[count].Cells["IsSelect"].Value = true;
this.myStyleDataGridView1.SelectAll();
}
else
{
this.myStyleDataGridView1.Rows[count].Cells["IsSelect"].Value = false;
}
}
getTotal();
}
View Code