Reputation: 119
I am trying to do the check box in the header will respond to any of checked or unchecked checkbox in the grid view rows. But, the check box header only respond when I check or uncheck the last checkbox in the gridview (The check box header will be checked if all of the checkbox in the gridview is checked and vice versa).
How can I solve not only last checkbox header which will respond to the last checkbox in the gridview, but every checkbox in the gridview?
Here is the code that I am using:
protected void checkAll_CheckedChanged(object sender, EventArgs e)
{
CheckBox ChkBoxHeader = (CheckBox)GridView1.HeaderRow.FindControl("checkAll");
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox ChkBoxRows = (CheckBox)row.FindControl("chkbx_select");
if (ChkBoxHeader.Checked && ChkBoxRows.Enabled)
{
ChkBoxRows.Checked = true;
}
else if (!ChkBoxHeader.Checked)
{
ChkBoxRows.Checked = false;
}
}
}
protected void chkbx_select_CheckedChanged(object sender, EventArgs e)
{
CheckBox ChkBoxHeader = (CheckBox)GridView1.HeaderRow.FindControl("checkAll");
bool isAllChecked = false;
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox checkBox = (CheckBox)row.FindControl("chkbx_select");
if (!checkBox.Checked && checkBox.Enabled)
{
isAllChecked = false;
}
else if (checkBox.Checked)
{
isAllChecked = true;
}
}
ChkBoxHeader.Checked = isAllChecked;
}
Your answer much appreciated.
Thank you.
Upvotes: 0
Views: 1091
Reputation: 8271
For checking Individual Check Box
protected void chkselect_CheckedChanged(object sender, EventArgs e)
{
try
{
int count = 0;
CheckBox checkall = (CheckBox)GridView1.HeaderRow.FindControl("mainchkselect");
foreach(GridViewRow gvrow in GridView1.Rows)
{
CheckBox chkindividual = (CheckBox)gvrow.FindControl("chkselect");
if (chkindividual.Checked)
count++;
//your logic
}
if (count == GridView1.Rows.Count )
{
checkall.Checked = true;
}
else
{
checkall.Checked = false;
}
}
catch (Exception ex)
{
ClientLogger.ClientErrorLogger(ex.Message);
}
}
For Checking Select All Check Box
protected void mainchkselect_CheckedChanged(object sender, EventArgs e)
{
try
{
CheckBox checkall = (CheckBox)GridView1.HeaderRow.FindControl("mainchkselect");
foreach(GridViewRow gvrrow in GridView1.Rows)
{
CheckBox chkindividual = (CheckBox)gvrrow.FindControl("chkselect");
if(checkall.Checked ==true)
{
chkindividual.Checked = true;
}
else
{
chkindividual.Checked = false;
}
}
}
catch(Exception ex)
{
ClientLogger.ClientErrorLogger(ex.Message);
}
}
Upvotes: 0
Reputation: 1282
Try this code.
If you check header checkbox.
protected void checkAll_CheckedChanged(object sender, EventArgs e)
{
CheckBox ChkBoxHeader = (CheckBox)GridView1.HeaderRow.FindControl("checkAll");
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox ChkBoxRows = (CheckBox)row.FindControl("chkbx_select");
if (ChkBoxRows!=null)
{
if(ChkBoxHeader.Checked)
ChkBoxRows.Checked = true;
else
ChkBoxRows.Checked = false;
}
}
}
When you check single checkbox.
protected void chkbx_select_CheckedChanged(object sender, EventArgs e)
{
CheckBox ChkBoxHeader = (CheckBox)GridView1.HeaderRow.FindControl("checkAll");
bool isAllChecked = false;
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox ChkBoxRows = (CheckBox)row.FindControl("chkbx_select");
if (!(ChkBoxRows.Checked && ChkBoxRows.Enabled))
{
isAllChecked = true;
}
}
if (!isAllChecked)
{
ChkBoxHeader.Checked = true;
}
}
Let me know if it works.
Upvotes: 0