Reputation: 3245
I have a question about if there is a better more efficient way to do what I want to accomplish:
What I want to do is loop through the rows in a grid view and check the check boxes for the first 100 records.
For this I came up with the solution below:
int limit = 0;
int max = 100;
foreach (GridViewRow gvr in GridView1.Rows)
{
limit++;
if (gvr.RowType == DataControlRowType.DataRow && limit <= max)
{
CheckBox cb = (CheckBox)gvr.FindControl("chkSelect");
cb.Checked = true;
}
}
I am wondering if this is the best way of doing this or if there is an easier/quicker way to do accomplish the same task.
Thanks for any help.
Upvotes: 0
Views: 593
Reputation: 684
Why you use a for-each loop if you dont want to check all rows? I would use a normal for loop like so:
int max = 100;
for (int i = 0; (i < GridView1.Rows.Count && i < max); i++)
{
GridViewRow gvr = GridView1.Rows[i];
if(gvr.RowType == DataControlRowType.DataRow){
CheckBox cb = (CheckBox)gvr.FindControl("chkSelect");
cb.Checked = true;
}
}
The check i < GridView1.Rows.Count
is optional, but prevent you from outofbounds exceptions.
Upvotes: 1