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); |
} |
} |
7 Answers, 1 is accepted
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.
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.
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
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>
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....
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