Checkboxes with Multi-Level Grouping

8 posts, 0 answers
  1. Taz
    Taz avatar
    15 posts
    Member since:
    Mar 2006

    Posted 15 Dec 2009 Link to this post

    Hello all,

    I just can't figure out how to add a checkbox to a second level (of a 3 level) grouping without loosing the data that is there.

    for instance 

    --Dept
    ----<check box here> Employee
    --------Projects


    I am able to add the check box like this:

     if (e.Item is GridGroupHeaderItem ) 
                { 
                     
                    GridGroupHeaderItem item = e.Item as GridGroupHeaderItem; 
     
                    if ( item.GroupIndex.LastIndexOf("_") == 1) 
                    { 
                         
                        DataRowView groupDataRow = (DataRowView)e.Item.DataItem; 
                        CheckBox check = new CheckBox(); 
                        check.AutoPostBack = false
                        check.ID = "foo"
                        check.Text = this._headerTextForEmployeeName + " FirstName - LastName";     
                     
                        check.CheckedChanged += new EventHandler(check_CheckedChanged); 
                        item.DataCell.Controls.Add(check); 
                     
                        } 
     
                    } 

    But this REPLACES the header text and value that was there!!
    Any ideas or suggestions?


  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 15 Dec 2009 Link to this post

    Hi Graham,

    Give a try with following code in order to add the checkbox control and see whether it helps.

    CS:
     
        protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
        { 
            if (e.Item is GridGroupHeaderItem) 
            { 
                GridGroupHeaderItem item = e.Item as GridGroupHeaderItem; 
                if (item.GroupIndex.LastIndexOf("_") == 1) 
                { 
                    DataRowView groupDataRow = (DataRowView)e.Item.DataItem; 
                    CheckBox check = new CheckBox(); 
                    check.AutoPostBack = false
                    check.ID = "foo"
                    item.Controls[1].Controls.Add(check); 
                } 
            }  
        } 

    -Shinu.
  3. Taz
    Taz avatar
    15 posts
    Member since:
    Mar 2006

    Posted 16 Dec 2009 Link to this post

    That did the trick. Thanks.
    Now to figure out how to get data from column XX only on rows where that checkbox is check during a postback,
    I guess I can iterate through the contros one at a time like I did to add the checkbox and look for a check box and it's state.


  4. SR
    SR avatar
    33 posts
    Member since:
    Dec 2013

    Posted 14 Mar 2014 in reply to Shinu Link to this post

    Hi Shinu

    I have Same Scenario but i need check boxes on first level and i  have one more check box in Grid Template column when i am check this select all check boxes in first level .

    Regards
    SR.
  5. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 14 Mar 2014 in reply to SR Link to this post

    Hi,

    Please take a look at the following code snippet to obtain your required scenario.

    ASPX:
    <telerik:GridTemplateColumn>
      <ItemTemplate>
          <asp:CheckBox ID="TemplateCheckBox" runat="server" />
      </ItemTemplate>
    </telerik:GridTemplateColumn>

    C#:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        CreateHeaderControls(e);
    }
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        CreateHeaderControls(e);
    }
     
    void check_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox check = (CheckBox)sender;
        GridGroupHeaderItem groupHeader = (GridGroupHeaderItem)check.NamingContainer;
        GridItem[] children = groupHeader.GetChildItems();
        foreach (GridItem child in children)
        {
            if (child is GridDataItem)
            {
                ((child as GridDataItem).FindControl("TemplateCheckBox") as CheckBox).Checked = (sender as CheckBox).Checked;
            }
        }
    }
    private void CreateHeaderControls(GridItemEventArgs e)
    {
        if (e.Item is GridGroupHeaderItem)
        {
            GridGroupHeaderItem item = e.Item as GridGroupHeaderItem;
            DataRowView groupDataRow = (DataRowView)e.Item.DataItem;
            CheckBox check = new CheckBox();
            check.AutoPostBack = true;
            check.ID = "CheckAll";
            check.Text = "Check/Uncheck CheckBox1 column";
            check.CheckedChanged += new EventHandler(check_CheckedChanged);
            item.DataCell.Controls.Add(check);
        }
    }

    Hope this helps.Let me know if any concern.
    Thanks,
    Shinu
  6. SR
    SR avatar
    33 posts
    Member since:
    Dec 2013

    Posted 14 Mar 2014 in reply to Shinu Link to this post

    Hi Shinu
    I am using Header Template  and i did the grouping with two levels 
    For EX:level 1 Dept,Level2 is Emp;
    now  i am try to add the check box in Level 1 when i am using this check box is came but the prob is second level also replace with checkbox and level1 group text my need is groupheader template with check box for level1 .

    when ever i am using this the above scenario repeated how to avoid this ?
     <GroupHeaderTemplate>
      <asp:CheckBox ID="chkHeader" runat="server" Text='<%# Eval("Dept") %>'></asp:CheckBox>
      </GroupHeaderTemplate>
  7. OZAN
    OZAN avatar
    1 posts
    Member since:
    Sep 2013

    Posted 03 Sep Link to this post

    Hi Shinhu

     

    You sample code works perfectly if there is a single group. When there are more then one however the following occurs:

    1) The innermost group checkbox works fine

    2) Second level group's checkbox does not work and makes the innermost group headers disappear completely

    3) Third level group checkbox completely crashes .exe

     

    The scenario I am using is dynamic grouping with the following sample scenario:

    Group A

    ------- Group B

    -------------- Group C

    -------------- Group C

    ------- Group B 

    -------------- Group C
    -------------- Group C

    Group A

    ------- Group B

    -------------- Group C
    -------------- Group C

    ------- Group B

    -------------- Group C
    -------------- Group C

     

    So in this scenario Group C checkbox works perfectly fine. Group B checkbox does nothing and makes Group C GroupHeader disappear completely. Data is there though. Group A crashes the app completely.

     

    If you can extend you example to handle this situation it would be awesome. Thanks in advance....

  8. Attila Antal
    Admin
    Attila Antal avatar
    386 posts

    Posted 06 Sep Link to this post

    Hi Ozan,

    To have the outermost Group checkbox have an effect, you will need to iterate through the items recursively. Here is an extension of the Code snippet provided by Shinu. Here is a short video for demonstration RadGrid GroupHeader Check in Multiple Level.

    void recursive (GridItem[] children, bool isChecked)
    {
        foreach (GridItem child in children)
        {
            if (child is GridDataItem)
            {
                ((child as GridDataItem).FindControl("TemplateCheckBox") as CheckBox).Checked = isChecked;
    
            }else if (child is GridGroupHeaderItem)
            {
                GridGroupHeaderItem groupHeader = child as GridGroupHeaderItem;
    
                recursive(groupHeader.GetChildItems(), isChecked);
            }
    
    
        }
    }
    
    void check_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox check = (CheckBox)sender;
        GridGroupHeaderItem groupHeader = (GridGroupHeaderItem)check.NamingContainer;
        GridItem[] children = groupHeader.GetChildItems();
        recursive(children, check.Checked);
    }
    private void CreateHeaderControls(GridItemEventArgs e)
    {
        if (e.Item is GridGroupHeaderItem)
        {
            GridGroupHeaderItem item = e.Item as GridGroupHeaderItem;
            DataRowView groupDataRow = (DataRowView)e.Item.DataItem;
            CheckBox check = new CheckBox();
            check.AutoPostBack = true;
            check.ID = "CheckAll";
            check.Text = "Check/Uncheck CheckBox1 column";
            check.CheckedChanged += new EventHandler(check_CheckedChanged);
            item.DataCell.Controls.Add(check);
        }
    }

     

    Please note that the Crashing part might happend due to other reasons or code logic. You will need to double check the logic you have in code behind and ensure that there are no conflicts while looping through the items.

     

    Kind regards,
    Attila Antal
    Progress Telerik

    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top