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

Dynamically add check boxes to Group Headers for Rad Grid.

1 Answer 206 Views
Grid
This is a migrated thread and some comments may be shown as answers.
suresh
Top achievements
Rank 1
suresh asked on 08 Jan 2014, 03:08 PM
Hi Team,
    I am trying to add check boxes in group header dynamically,i found some code  http://www.telerik.com/community/forums/aspnet-ajax/ajax/groupging-columns-with-check-boxes.aspx in this link when i try with it i am unable to click the check boxes in header (disable mode) can you please help me.

Team, 
   Please find the attached screen shot of my requirement.high lighted area should get check boxes.

Using in aspx 

<telerik:RadGrid ID="gvSRSecurity" AutoGenerateColumns="false" runat="server" CellSpacing="0" ShowGroupPanel="true"  OnItemCreated="RadGrid1_ItemCreated"
          OnItemDataBound="RadGrid1_ItemDataBound"  AllowMultiRowSelection="True"
                            GridLines="None" Width="100%" PagerStyle-AlwaysVisible="true">
                            <ClientSettings>
                                <Scrolling AllowScroll="True" UseStaticHeaders="True"></Scrolling>
                            </ClientSettings>
                            <MasterTableView AutoGenerateColumns="False" TableLayout="fixed" DataKeyNames="ReportId"
                                GroupsDefaultExpanded="True" GroupLoadMode="Client" CommandItemDisplay="None">                              
                                <GroupByExpressions>
                                
                                    <telerik:GridGroupByExpression>
                                        <SelectFields>
                                            <telerik:GridGroupByField FieldName="DomainName" FieldAlias="Domain" FormatString=""
                                                HeaderText=" " HeaderValueSeparator=""></telerik:GridGroupByField>
                                                
                                        </SelectFields>
                                        <GroupByFields>
                                        
                                            <telerik:GridGroupByField SortOrder="Descending" FieldName="DomainName" FormatString=""
                                                HeaderText=""></telerik:GridGroupByField>
                                        </GroupByFields>
                                    </telerik:GridGroupByExpression>
                                    <telerik:GridGroupByExpression>
                                        <SelectFields>
                                            <telerik:GridGroupByField FieldName="SubjectAreaName" FieldAlias="SubjectArea" FormatString=""
                                                HeaderText=" " HeaderValueSeparator=""></telerik:GridGroupByField>
                                        </SelectFields>
                                        <GroupByFields>
                                            <telerik:GridGroupByField SortOrder="Descending" FieldName="SubjectAreaName" FormatString=""
                                                HeaderText=""></telerik:GridGroupByField>
                                        </GroupByFields>
                                    </telerik:GridGroupByExpression>
                                    <telerik:GridGroupByExpression>
                                        <SelectFields>
                                            <telerik:GridGroupByField FieldName="GroupName" FieldAlias="Group" FormatString=""
                                                HeaderText=" " HeaderValueSeparator=""></telerik:GridGroupByField>
                                        </SelectFields>
                                        <GroupByFields>
                                            <telerik:GridGroupByField SortOrder="Descending" FieldName="GroupName" FormatString=""
                                                HeaderText=""></telerik:GridGroupByField>
                                        </GroupByFields>
                                    </telerik:GridGroupByExpression>
                                   
                                </GroupByExpressions>
                                <Columns>
                              <%--   <telerik:GridCheckBoxColumn UniqueName="CheckBox1" HeaderText="CheckBox1"></telerik:GridCheckBoxColumn>
           <telerik:GridCheckBoxColumn UniqueName="CheckBox2" HeaderText="CheckBox2"></telerik:GridCheckBoxColumn>--%>
                                </Columns>
                            </MasterTableView>
                        </telerik:RadGrid>

in Cs file

 GridTemplateColumn tempcol = new GridTemplateColumn();
                //GridHeaderItem 
                //GridHeaderItem ghi = new GridHeaderItem();

                tempcol.ItemTemplate = new MySRCheckbox(i, true);
                tempcol.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                tempcol.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
                tempcol.ItemStyle.Width = Unit.Pixel(60);
                tempcol.HeaderStyle.Width = Unit.Pixel(80);
                tempcol.HeaderText = cols[i];                
                gvSRSecurity.MasterTableView.Columns.Add(tempcol);

   --check boxes code which i found in above link
    #region add code for grouping

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {

        CreateHeaderControls(e);
    }

    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridCommandItem)
        {
            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)["CheckBox1"].Controls[0] as CheckBox).Checked = (sender as CheckBox).Checked;
            }
        }
    }
    private void CreateHeaderControls(GridItemEventArgs e)
    {
        if (e.Item is GridGroupHeaderItem)
        {
            DataTable dtSRActions;
            dtSRActions = (DataTable)Session["dtSRActions"];
            List<string> cols = (from perm in dtSRActions.AsEnumerable()
                                 select perm.Field<string>("SRPermissionName").ToString()).ToList<string>();

         

            for (int i = 0; i < cols.Count; i++)
            {
                
                GridGroupHeaderItem item = e.Item as GridGroupHeaderItem;
                item.DataCell.Controls.Clear();
                DataRowView groupDataRow = (DataRowView)e.Item.DataItem;
                CheckBox check = new CheckBox();
                check.AutoPostBack = true;
              //  check.Enabled = true;
                check.ID = "CheckAll" + i;
                check.Text = ""; //cols[i];// "Check/Uncheck CheckBox1 column";
                check.CheckedChanged += new EventHandler(check_CheckedChanged);

                CheckBox check2 = new CheckBox();
                check2.AutoPostBack = true;
                check2.ID = "CheckAll_2" + i;
               // check.Enabled = false;
                check2.Text = "";// cols[i];//"Check/Uncheck CheckBox2 column";
                //check2.CheckedChanged += check2_CheckedChanged;
                check2.CheckedChanged +=new EventHandler(check2_CheckedChanged);                   
                item.DataCell.Controls.Add(check);
                item.DataCell.Controls.Add(check2);
            }            
            //Add CheckBox and event handler for every column 
        }
    }

    void check2_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)["CheckBox2"].Controls[0] as CheckBox).Checked = (sender as CheckBox).Checked;
            }
        }
    }


    #endregion



Thank you.

1 Answer, 1 is accepted

Sort by
0
Pavlina
Telerik team
answered on 13 Jan 2014, 03:17 PM
Hello,

I tested the provided code in a sample test page and it is working as expected. Only the checkbox column is disable which is expected, because you can click it only in edit mode. However, the checkbox which is added in the header is not in disable mode and I can check it without problems. You can find the project attached and see if it helps.

Regards,
Pavlina
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
suresh
Top achievements
Rank 1
Answers by
Pavlina
Telerik team
Share this question
or