Consider the current algorithm below that iterates through a GridView
's rows to find whether the contained Checkbox
is selected/checked.
List<int> checkedIDs = new List<int>();
foreach (GridViewRow msgRow in messagesGrid.Rows)
{
CheckBox chk = (CheckBox)msgRow.FindControl("chkUpdateStatus");
if (chk.Checked){
//we want the GridViewRow's DataKey value
checkedMsgIDs.Add(int.Parse(messagesGrid.DataKeys[msgRow.RowIndex].Value.ToString()));
}
}
This works as expected: you're left with a fully populated List<int>
.
Question: How would you or could you re-write or improve this algorithm using LINQ to search the GridView
for all the rows who have their Checkbox
selected/checked?