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

Checkbox in nested grid should check children

5 Answers 147 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jeanne Kornkven
Top achievements
Rank 1
Jeanne Kornkven asked on 26 May 2010, 11:27 PM
I have a 3 level nested RadGrid.
All levels have checkboxes.
If the checkbox at any level is checked, the checkboxes of its children (and grandchildren) should also be checked.
How can I accomplish this? 

5 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 27 May 2010, 07:43 AM
Hello Jeanne,

Check out the following code snippet which i tried in my application for a similar scenario.

ASPX:
 <MasterTableView Name="Master" runat="server" TableLayout="Auto" DataKeyNames="OrderID" 
                DataSourceID="SqlDataSource3" HierarchyLoadMode="Client"
                <DetailTables> 
                    <telerik:GridTableView HierarchyLoadMode="Client" runat="server" AutoGenerateColumns="true" 
                        DataKeyNames="EmployeeID" Name="DetailTableView1" DataSourceID="SqlDataSource1"
                        <Columns> 
                            <telerik:GridTemplateColumn> 
                                <ItemTemplate> 
                                    <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True" OnCheckedChanged="CheckBox2_CheckedChanged" /> 
                                </ItemTemplate> 
                            </telerik:GridTemplateColumn> 
                        </Columns> 
                        <ParentTableRelation> 
                            <telerik:GridRelationFields DetailKeyField="EmployeeID" MasterKeyField="EmployeeID" /> 
                        </ParentTableRelation> 
                        <DetailTables> 
                            <telerik:GridTableView runat="server" DataSourceID="SqlDataSource2" Name="DetailTableView2" 
                                DataKeyNames="EmployeeID"
                                <Columns> 
                                    <telerik:GridTemplateColumn> 
                                        <ItemTemplate> 
                                            <asp:CheckBox ID="CheckBox3" runat="server" /> 
                                        </ItemTemplate> 
                                    </telerik:GridTemplateColumn> 
                                </Columns> 
                                <ParentTableRelation> 
                                    <telerik:GridRelationFields MasterKeyField="EmployeeID" DetailKeyField="EmployeeID" /> 
                                </ParentTableRelation> 
                            </telerik:GridTableView> 
                        </DetailTables> 
                        <ExpandCollapseColumn Visible="True"
                        </ExpandCollapseColumn> 
                    </telerik:GridTableView> 
                </DetailTables> 
                <ExpandCollapseColumn Visible="True"
                </ExpandCollapseColumn> 
                <Columns> 
                    <telerik:GridTemplateColumn> 
                        <ItemTemplate> 
                            <asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" 
                                AutoPostBack="True" /> 
                        </ItemTemplate> 
                    </telerik:GridTemplateColumn> 
                </Columns> 
            </MasterTableView> 

C#:
 
 protected void CheckBox1_CheckedChanged(object sender, EventArgs e) 
    { 
        CheckBox chk1 =(CheckBox)sender; 
        GridDataItem item = (GridDataItem)chk1.NamingContainer; 
        foreach (GridDataItem childitems in item.ChildItem.NestedTableViews[0].Items) 
        { 
            CheckBox chk2 = (CheckBox)childitems.FindControl("CheckBox2"); 
            chk2.Checked = chk1.Checked; 
            foreach (GridDataItem grandchilditems in childitems.ChildItem.NestedTableViews[0].Items) 
            { 
                CheckBox chk3 = (CheckBox)grandchilditems.FindControl("CheckBox3"); 
                chk3.Checked = chk1.Checked; 
            } 
        } 
    } 

Also write the code for CheckedChanged event of CheckBox2, based on same logic.

Cheers,
Shinu.



0
Jeanne Kornkven
Top achievements
Rank 1
answered on 27 May 2010, 06:11 PM

Shinu,
     Thank you.  Your code works when the checkbox is in a grid template column.  However, I am using the GridClientSelectColumn.  Is there a way to make this happen for that column type?

Thanks,,

Jeanne

0
Jeanne Kornkven
Top achievements
Rank 1
answered on 28 May 2010, 06:09 PM
Ah, Shinu, I got it working It seems that the GridClientSelectColumn checkboxes must have the id "selColumnSelectCheckBox" in order to work properly.
Thank you.

 

 Protected Sub dgChkBox_StyleCheckChanged(ByVal sender As ObjectByVal e As System.EventArgs)  
        'Check/uncheck the colors and sizes that are below the style  
        Dim chkStyle As CheckBox = DirectCast(sender, CheckBox)  
        Dim item As GridDataItem = DirectCast(chkStyle.NamingContainer, GridDataItem)  
        For Each childitems As GridDataItem In item.ChildItem.NestedTableViews(0).Items  
            Dim chkColor As CheckBox = DirectCast(childitems.FindControl("selColumnSelectCheckBox"), CheckBox)  
            If chkColor IsNot Nothing Then 
                childitems.Selected = chkStyle.Checked  
                chkColor.Checked = chkStyle.Checked  
                For Each grandchilditems As GridDataItem In childitems.ChildItem.NestedTableViews(0).Items  
                    Dim chkSize As CheckBox = DirectCast(grandchilditems.FindControl("selColumnSelectCheckBox"), CheckBox)  
                    If chkSize IsNot Nothing Then 
                        grandchilditems.Selected = chkStyle.Checked  
                        chkSize.Checked = chkStyle.Checked  
                    End If 
                Next 
            End If 
        Next 
    End Sub 
 
    Public Sub dgChkBox_ColorCheckChanged(ByVal sender As ObjectByVal e As System.EventArgs)  
        'Check/uncheck the sizes that are below the Color  
        Dim chkColor As CheckBox = DirectCast(sender, CheckBox)  
        Dim item As GridDataItem = DirectCast(chkColor.NamingContainer, GridDataItem)  
        For Each childitems As GridDataItem In item.ChildItem.NestedTableViews(0).Items  
            Dim chkSize As CheckBox = DirectCast(childitems.FindControl("selColumnSelectCheckBox"), CheckBox)  
            If chkSize IsNot Nothing Then 
                childitems.Selected = chkColor.Checked  
                chkSize.Checked = chkColor.Checked  
            End If 
        Next 
    End Sub 
 

 


 
0
Yavor
Telerik team
answered on 02 Jun 2010, 07:20 AM
Hello Jeanne,

Indeed, this is the proper way to handle the selection and the location of the checkbox when using a client select column. The general logic is to follow the approach used in this topic:

http://www.telerik.com/help/aspnet-ajax/grdaccessingcellsandrows.html

I hope that the setup is working as per your requitements now. Let us know if further questions arise.

Kind regards,
Yavor
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Yavor
Telerik team
answered on 02 Jun 2010, 08:23 AM
Hi Jeanne,

As a quick follow up on the issue discussed in this thread, I wanted to add that the functionality in question can also be handled on the client. This is demonstrated in the code sample, attached to this message. I hope it helps.

Regards,
Yavor
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
Jeanne Kornkven
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Jeanne Kornkven
Top achievements
Rank 1
Yavor
Telerik team
Share this question
or