foreach (GridViewRow row in gridView.Rows)
{ // Access the CheckBox
CheckBox cb = (CheckBox)row.FindControl("SuburbSelector");
if (cb.Checked)
{
//do something;
}
}
I tried the following and got error
Linq:
var Str = SuburbGridView.Rows.Cast<GridViewRow>().Where(r=>(CheckBox)r.FindControl("SuburbSelector")==checked);
Error:
Delegate ‘System.Func < System.Web.UI.WebControls.GridViewRow,int,bool>’ does not take 1 arguments
Many thanks
Linq doesn’t seem to have a good “each” aggregate. There is
Aggregate(), which I don’t like if I’m not actually accumulating anything, since the accumulator value is essentially thrown away. I don’t think there’s anything equivalent toList<T>.ForEach(), and it’s a shame. If you’re using C# 4.0, and don’t mind processing in parallel, you could use.AsParallel().ForAll(). So anyway, here’s a few ways to do what you want:Using
List.ForEach():And using Parallel Linq:
I’m just getting the hang of this LINQ stuff myself. I love it.
By the way, you were just missing an extra pair of
()‘s around your cast.FindControl()returns aSystem.Web.UI.Control, and you have to cast it toCheckBoxto access theCheckedproperty. You can’t do it like this:That doesn’t work because of the order of operations. The
.Checkedis evaluated before the cast toCheckBox, which means you’re trying to access aCheckBoxproperty ofControl, and it doesn’t exist. The extra pair of parens fixes that.And one last thing, you don’t need to do
== truein your comparison.Checkedis already a boolean value. If you like it there for clarity (some people do), then by all means keep it. It’s not considered bad practice or anything, it’s just a matter of preference.