esko_user
asked on
Get the selected checkboxs count on click in Datagridview
I need to get the count of the check boxes which are selected, in a datagridview.
I have added the check box column through code,
DataColumn colSelection = new DataColumn();
colSelection.DataType = System.Type.GetType("Syste m.Boolean" );
colSelection.ColumnName = "Select";
this.objdatatable.Columns. Add(colSel ection);
this.dataGridViewResources .DataSourc e = this.objdatatable;
To get the count i have called a method on the cellContentClick event . Please refer the below code. The code gives me correct count when the check boxes are selected. But if i unCheck the check boxes the count is not correct (as chkActive.Selected remains true even if i uncheck the check box)
any suggestions?
I have added the check box column through code,
DataColumn colSelection = new DataColumn();
colSelection.DataType = System.Type.GetType("Syste
colSelection.ColumnName = "Select";
this.objdatatable.Columns.
this.dataGridViewResources
To get the count i have called a method on the cellContentClick event . Please refer the below code. The code gives me correct count when the check boxes are selected. But if i unCheck the check boxes the count is not correct (as chkActive.Selected remains true even if i uncheck the check box)
any suggestions?
//method call on cell content click event
private void dataGridViewResources_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
string selectedCount = this.GetSelectedCount();
lblSelectedCount.Text = selectedCount;
}
}
//method which returns the count of the check box selected
private string GetSelectedCount()
{
try
{
for (int i = 0; i <= dataGridViewResources.Rows.Count - 1; i++)
{
DataGridViewCheckBoxCell chkActive = (DataGridViewCheckBoxCell)dataGridViewResources.Rows[i].Cells[0];
if (chkActive.Selected == true)
{
selectedCount = selectedCount + 1;
}
}
}
catch (Exception ex)
{
}
return selectedCount.ToString();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.