Iterate over rows/checkboxes in a RadGrid

ChessWhiz picture ChessWhiz · Apr 6, 2010 · Viewed 16.4k times · Source

I have a Telerik RadGrid with a GridTemplateColumn that contains a checkbox, as follows:

<telerik:GridTemplateColumn HeaderText="MINE" UniqueName="MyTemplateColumn">
     <ItemTemplate>
          <asp:CheckBox id="MyCheckBox" runat="server"></asp:CheckBox>
     </ItemTemplate>
</telerik:GridTemplateColumn>

I want to set the box to be "checked" based on a value read from the database. I could handle the ItemDataBound event and read the database when each row is bound, but that results in n lookups. Instead, I want to handle DataBound, and then set all the values at once. So, in that method, I want code like this:

// read all values from database first, then...
foreach(var chkbox in MyRadGrid.MasterTableView.Columns.FindByUniqueName("MyTemplateColumn").FindControl("MyCheckBox")) {
    chkbox.Checked = oneValue;
}

That doesn't work, because FindControl isn't a method of GridColumn, and it won't generate an iterable list of the checkboxes. What is the correct way to iterate through the checkboxes in the template column? Thanks!

Answer

ChessWhiz picture ChessWhiz · Apr 7, 2010

Telerik got back to me on their forums with the answer, as follows:

foreach (GridDataItem item in MyRadGrid.MasterTableView.Items) 
{ 
  CheckBox chk = (CheckBox)item.FindControl("MyCheckBox");
  // Set the value here
}

Hope this is useful for someone!