I want to program the checkboxes so that if a row is selected, the program will read the value in the first column of the gridview row and automatically check all rows that also have that value in their first column. I was able to do this using an array to store the value and running through each row, checking if this value was a match. If it matched, the checkbox was checked. This worked but was a very limited solution. When I tried to reverse the action, it simply rechecked that value automatically because that value was still in the array and I did not know how to distinguish between a check or un-check action. It was solely based on a change event.
int count = 0;
foreach (GridViewRow gvrow in GridView1.Rows)
{
CheckBox chk = (CheckBox)gvrow.FindControl("chkRow");
if (chk.Checked)
{
count++;
}
}
string [] dnum = new string[count];
int counter = 0;
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox myCheckBox = row.FindControl("chkRow") as CheckBox;
if (myCheckBox.Checked)
{
if (counter > 0)
{
int number = counter - 1;
if (row.Cells[1].Text != dnum[number])
{
dnum[counter] = row.Cells[1].Text;
counter++;
}
}
else
{
dnum[counter] = row.Cells[1].Text;
counter++;
}
}
}
return dnum;
}
The array dnum should return the first column values for the checked rows . With this I can run through each row and check if any checkboxes need to be checked.
foreach (GridViewRow gvrow in GridView1.Rows)
{
CheckBox ChkBox = (CheckBox)gvrow.FindControl("chkRow");
if (ChkBox.Checked == true)
{
foreach (string s in first)
{
if (gvrow.Cells[1].Text == s)
{
ChkBox.Checked = true;
}
}
}
But now I am unable to figure out how to reverse the process, i.e when I uncheck one, all with the same first column value must uncheck, instead it just rechecks because that value is still in the array. I am open to completely different methods.
Many thanks, Nicolas
Aucun commentaire:
Enregistrer un commentaire