This is a migrated thread and some comments may be shown as answers.

Getting controls in detail table item template

1 Answer 265 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Utkarsh Shigihalli
Top achievements
Rank 1
Utkarsh Shigihalli asked on 21 Oct 2010, 02:28 PM
Hello,

I have a grid with detail table inside it. I have check box in header column in it which provides "select all" functionality, i.e I want to provide the functionality where clicking on header check box selects all the check box in the column of detail table.
I looked at this Example in the site, but some how I was not able to get the "CheckBox1" control inside item template of the detail table. Please help. Following is the code I have in aspx side.
<telerik:RadGrid runat="server" AllowMultiRowSelection="false" AutoGenerateColumns="false"
    ID="radGridChecks" AllowSorting="true" AllowPaging="true" PageSize="7" AllowFilteringByColumn="false"
    HeaderStyle-HorizontalAlign="Center" GroupingSettings-CaseSensitive="false" AllowAutomaticDeletes="false"
    AllowAutomaticInserts="false" AllowAutomaticUpdates="false" OnItemDataBound="OnRadGridChecks_ItemDataBound"
    OnPageIndexChanged="OnRadGridChecks_PageIndexChanged" OnNeedDataSource="OnRadGridCheck_NeedDataSource"
    OnDetailTableDataBind="OnRadGridCheck_OnDetailTableDataBind" OnItemCommand="OnRadGridCheck_OnItemCommand">
    <MasterTableView CommandItemDisplay="Top" EnableColumnsViewState="false" AllowAutomaticDeletes="false"
        AllowAutomaticInserts="false" AllowAutomaticUpdates="false" Name="ChecksByRepGrid"
        DataKeyNames="RepCode" ItemStyle-HorizontalAlign="Center">
        <CommandItemSettings ShowAddNewRecordButton="false" />
        <Columns>
            <telerik:GridBoundColumn DataField="RepName" HeaderText="Name" />
            <telerik:GridBoundColumn DataField="Count" HeaderText="# of Checks" />
        </Columns>
        <DetailTables>
            <telerik:GridTableView align="right" DataKeyNames="Code" runat="server" Name="ChecksByRepChildGrid"
                AutoGenerateColumns="false" AllowFilteringByColumn="false">
                <ParentTableRelation>
                    <telerik:GridRelationFields DetailKeyField="Code" MasterKeyField="RepCode" />
                </ParentTableRelation>
                <Columns>
                    <telerik:GridBoundColumn DataField="ClientName" HeaderText="Client Name">
                        <HeaderStyle HorizontalAlign="Center" />
                        <ItemStyle HorizontalAlign="Center" />
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Status" HeaderText="Status">
                        <HeaderStyle HorizontalAlign="Center" />
                        <ItemStyle HorizontalAlign="Center" />
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="CheckNumber" HeaderText="Check #">
                        <HeaderStyle HorizontalAlign="Center" />
                        <ItemStyle HorizontalAlign="Center" />
                    </telerik:GridBoundColumn>
                     
                    <telerik:GridBoundColumn DataField="PayableTo" HeaderText="Payable To">
                        <HeaderStyle HorizontalAlign="Center" />
                        <ItemStyle HorizontalAlign="Center" />
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="ForwardedTo" HeaderText="Forwarded To">
                        <HeaderStyle HorizontalAlign="Center" />
                        <ItemStyle HorizontalAlign="Center" />
                    </telerik:GridBoundColumn>
                    <telerik:GridTemplateColumn UniqueName="CheckBoxTemplateColumn">
                            <HeaderTemplate>
                             <asp:CheckBox id="headerChkbox" OnCheckedChanged="ToggleSelectedState" AutoPostBack="True" runat="server"></asp:CheckBox>
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:CheckBox id="CheckBox1" OnCheckedChanged="ToggleRowSelection" AutoPostBack="True" runat="server"></asp:CheckBox>
                            </ItemTemplate>
                            <HeaderStyle HorizontalAlign="Center" />
                            <ItemStyle HorizontalAlign="Center" />
                        </telerik:GridTemplateColumn>
                    
                </Columns>
            </telerik:GridTableView>
        </DetailTables>
    </MasterTableView>
 
</telerik:RadGrid>

Thanks
Utkarsh
http://www.utkarshonline.com

1 Answer, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 22 Oct 2010, 08:58 AM
Hello Utkarsh,

The following code snippet shows how to access the CheckBox inside ItemTemplate of  DetailTable.

C#:
protected void ToggleSelectedState(object sender, EventArgs e)
  {
      CheckBox headerCheckBox = (sender as CheckBox);
      GridHeaderItem headeritem = (GridHeaderItem)headerCheckBox.NamingContainer;
      GridDataItem parentItem = (GridDataItem)headeritem.OwnerTableView.ParentItem;
      GridTableView detailTable = (GridTableView)parentItem.ChildItem.NestedTableViews[0];
      foreach (GridDataItem dataItem in detailTable.Items)// loops through each DetailTable item
      {
          (dataItem.FindControl("CheckBox1") as CheckBox).Checked = headerCheckBox.Checked;// accessing checkbox in DetailTable
          dataItem.Selected = headerCheckBox.Checked;
      }
  }

Thanks,
Princy
Tags
Grid
Asked by
Utkarsh Shigihalli
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or