0
我使用以下代码来获取在datagridview中选择的员工总数。 我希望它在用户选中复选框或用户取消选中时进行计算。将CellDirtyStateChanged事件与CheckBoxColumns一起使用
private void dgvEmployeesToProcess_CellDirtyStateChanged(object sender, EventArgs e)
{
try
{
if (dgvEmployeesToProcess.CurrentCell.ColumnIndex == 4)
{
txtNoOfEmpToProcess.Text = string.Empty;
int count = 0;
foreach (DataGridViewRow row in dgvEmployeesToProcess.Rows)
{
DataGridViewCheckBoxCell checkBox = (DataGridViewCheckBoxCell)row.Cells[4];
if (Convert.ToBoolean(checkBox.Value) == true)
{
count = count + 1;
}
}
// show total count in textbox
txtNoOfEmpToProcess.Text = count.ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
我遇到的问题是,当我检查行[0]中的复选框,即。第一个,checkBox.Value
总是空,count = 0.当我点击第二个复选框时,count = 1等等。 为了使计数正确,我需要做些什么?我需要使用CellDirtyStateChanged
以外的东西吗?