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

Adding controls to GridGroupHeaderItem on ItemCreated

1 Answer 323 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 23 Mar 2011, 04:05 PM
Hi.
I have the following scenario:
I'm presenting grouped data in a RadGrid and on the group headers I would like to include an edit button for editing the group concept. The group concept in this case is units.
The sub data under units should also have an edit button, but this i can achieve.

I currently have two problems:
Whenever I try to add a control according to any of the examples i find in the forums, the control appears underneath/behind the minimize icon. I can't figure out how to put the control at the rightmost position in the header row.

My second problem is that i don't really understand how I'm suppose to pass the required value of my concept id (unitId) to the GroupHeaderItem in the ItemCreated event.

I have tried to subtract the code from my project and attached it below.

Best regards
Richard Furberg

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
  
public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
  
    }
    public class ExtendedRadGrid : RadGrid
    {
        public Int32 Year { get; set; }
    }
  
    protected void Page_Init(Object sender, EventArgs e)
    {
        RadScriptManager radScriptManager = new RadScriptManager();
        this.form1.Controls.Add(radScriptManager);
        RadAjaxLoadingPanel radAjaxLoadingPanel = new RadAjaxLoadingPanel() { ID = "radAjaxLoadingPanel", Skin = "Default" };
        RadAjaxPanel radAjaxPanel = new RadAjaxPanel() { LoadingPanelID = "radAjaxLoadingPanel" };
        RadTabStrip radTabStrip = new RadTabStrip() { Skin = "Default", AutoPostBack = true, MultiPageID = "radMultiPage", SelectedIndex = 0 };
        RadMultiPage radMultiPage = new RadMultiPage() { RenderSelectedPageOnly = true, ID = "radMultiPage", SelectedIndex = 0 };
  
        int[] years = new int[] { 2010, 2011 };
  
        foreach (int year in years)
        {
            radTabStrip.Tabs.Add(new RadTab(year.ToString(), "RadTabID" + year.ToString()) { PageViewID = "PageViewID" + year.ToString() });
  
            ExtendedRadGrid radGrid = new ExtendedRadGrid();
  
            radGrid.PageSize = 20;
            radGrid.Skin = "Windows7";
            radGrid.Width = Unit.Percentage(100);
            radGrid.PagerStyle.Mode = GridPagerMode.Slider;
            radGrid.MasterTableView.TableLayout = GridTableLayout.Fixed;
            radGrid.Year = year;
            radGrid.NeedDataSource += new GridNeedDataSourceEventHandler(radGrid_NeedDataSource);
            radGrid.FilterMenu.ExpandAnimation.Type = AnimationType.None;
            radGrid.FilterMenu.CollapseAnimation.Type = AnimationType.None;
            radGrid.AllowSorting = true;
            radGrid.AllowPaging = true;
            radGrid.ShowStatusBar = true;
            radGrid.ShowFooter = true;
            radGrid.AllowFilteringByColumn = true;
            radGrid.FilterMenu.EnableShadows = true;
            radGrid.FilterMenu.EnableRoundedCorners = true;
            radGrid.ClientSettings.AllowDragToGroup = false;
            radGrid.AutoGenerateColumns = false;
            radGrid.ShowGroupPanel = false;
            radGrid.GroupingEnabled = true;
            radGrid.GroupingSettings.GroupByFieldsSeparator = "    -    ";
            radGrid.GroupHeaderItemStyle.CssClass = "HeaderItem";
            radGrid.ItemCreated += RadGrid1_ItemCreated;
            radGrid.MasterTableView.DataKeyNames = new string[] { "RoleCompensationID", "UnitCompensationID" };
            //----------------------------------------------------------------------------------
            //Bound columns
  
            GridBoundColumn gridBoundColumn;
  
            gridBoundColumn = new GridBoundColumn();
            gridBoundColumn.DataField = "Title";
            gridBoundColumn.HeaderText = "Title";
            gridBoundColumn.FooterText = "Title";
            radGrid.MasterTableView.Columns.Add(gridBoundColumn);
  
            GridTemplateColumn gridTemplateColumn = new GridTemplateColumn();
            radGrid.MasterTableView.Columns.Add(gridTemplateColumn);
  
            //----------------------------------------------------------------------------------
            //GroupByExpressions
  
            GridGroupByExpression expression = new GridGroupByExpression();
            GridGroupByField gridGroupByField;
  
            //SelectFields
            gridGroupByField = new GridGroupByField();
            gridGroupByField.FieldName = "UnitName";
            gridGroupByField.HeaderText = " ";
            gridGroupByField.HeaderValueSeparator = " ";
            expression.SelectFields.Add(gridGroupByField);
  
            gridGroupByField = new GridGroupByField();
            gridGroupByField.FieldName = "UnitCompensationID";
            gridGroupByField.HeaderText = " ";
            gridGroupByField.HeaderValueSeparator = " ";
            gridGroupByField.FormatString = " ";
            expression.SelectFields.Add(gridGroupByField);
  
            //GroupByField
            gridGroupByField = new GridGroupByField();
            gridGroupByField.FieldName = "UnitName";
            gridGroupByField.HeaderText = " ";
            gridGroupByField.HeaderValueSeparator = " ";
            expression.GroupByFields.Add(gridGroupByField);
  
            radGrid.MasterTableView.GroupByExpressions.Add(expression);
            //----------------------------------------------------------------------------------
  
            RadPageView radPageView = new RadPageView() { ID = "PageViewID" + year };
            radPageView.Controls.Add(radGrid);
            radMultiPage.PageViews.Add(radPageView);
        }
  
        radAjaxPanel.Controls.Add(new LiteralControl("<div style='margin-bottom:-1px;'>"));
        radAjaxPanel.Controls.Add(radTabStrip);
        radAjaxPanel.Controls.Add(new LiteralControl("</div>"));
  
        radAjaxPanel.Controls.Add(radMultiPage);
        this.form1.Controls.Add(radAjaxLoadingPanel);
        this.form1.Controls.Add(radAjaxPanel);
  
    }
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem dataItem = e.Item as GridDataItem;
            dataItem.Attributes.Add("onClick", "alert('" + dataItem.GetDataKeyValue("RoleCompensationID") + "')"); // No problem getting the required id value.
        }
        else if (e.Item is GridGroupHeaderItem)
        {
            GridGroupHeaderItem gridGroupHeaderItem = e.Item as GridGroupHeaderItem;
            DataRowView groupDataRow = (DataRowView)gridGroupHeaderItem.DataItem;
            Button objButton = new Button();
            objButton.CommandName = "Add";
            objButton.ID = "Button_Add";
            objButton.Text = groupDataRow["UnitCompensationID"].ToString(); // Here i created an extra gridGroupByField in selectFields with an empty FormatString for passing ID value. I'm sure there must be a better way...
            ((GridTableCell)gridGroupHeaderItem.Controls[0]).Controls.Add(objButton); 
        }
    }
  
    void radGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        //Here I'm retrieving and setting a DataTable as DataSource
    }
}

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 24 Mar 2011, 11:35 AM
Hello Richard,

 Try the following code snippet in itemDataBound event to add controls to GroupHeaderItem.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridGroupHeaderItem)
            {
                GridGroupHeaderItem item = (GridGroupHeaderItem)e.Item;
                Button objButton = new Button();
                  objButton.CommandName = "Add";
                objButton.ID = "Button_Add";
                item.DataCell.Controls.Add(objButton);
                item.DataCell.Style["text-align"] = "right";
            }
        }

And I am not quite sure about your second requirement. Can you please elaborate it?

-Shinu.
Tags
Grid
Asked by
Richard
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or