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

Checkboxes with Multi-Level Grouping

7 Answers 436 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Taz
Top achievements
Rank 1
Taz asked on 15 Dec 2009, 04:52 PM
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?


7 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 16 Dec 2009, 05:21 AM
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.
0
Taz
Top achievements
Rank 1
answered on 16 Dec 2009, 07:49 PM
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.


0
SR
Top achievements
Rank 1
answered on 14 Mar 2014, 07:10 AM
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.
0
Shinu
Top achievements
Rank 2
answered on 14 Mar 2014, 08:41 AM
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
0
SR
Top achievements
Rank 1
answered on 14 Mar 2014, 11:47 AM
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>
0
OZAN
Top achievements
Rank 1
answered on 03 Sep 2019, 08:29 AM

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....

0
Attila Antal
Telerik team
answered on 06 Sep 2019, 08:10 AM

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.
Tags
Grid
Asked by
Taz
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Taz
Top achievements
Rank 1
SR
Top achievements
Rank 1
OZAN
Top achievements
Rank 1
Attila Antal
Telerik team
Share this question
or