pivot grid Cell template programatically

5 posts, 0 answers
  1. Syed
    Syed avatar
    36 posts
    Member since:
    Sep 2013

    Posted 13 Oct 2013 Link to this post

    Hi,
    Iam creating a Class for Pivot grid for programatic creation of grid.
    Example :

     public static void Layouts(RadPivotGrid grid, int height, int width, Boolean widthtype, Boolean allowsorting)
        {
       
            grid.Skin = "Office2007";// set the Skin

            grid.EmptyValue = "No data";     
              
            grid.Height = height; //Set the height of the grid  in % or in pixcel
            if (width > 0)
            {
                if (widthtype == false)
                {
                    grid.Width = width; // set the Width of the grid  in % or in pixcel
                }
                else
                {
                    grid.Width = Unit.Percentage(width);
                }
            }
            grid.AllowSorting = allowsorting;
            grid.AllowFiltering = false;
            grid.ShowFilterHeaderZone = false;
            grid.ShowDataHeaderZone = false;
            grid.ShowFilterHeaderZone = false;
            grid.ShowRowHeaderZone = false;
            grid.ClientSettings.Scrolling.AllowVerticalScroll = true;
            grid.ClientSettings.Scrolling.SaveScrollPosition = true;

        }

        #endregion

        //Set all the telerik Grid Page
        #region LayoutPage
        public static void LayoutPage(RadPivotGrid grid, int pagesize, Boolean allowpaging)
        {
            grid.PageSize = pagesize;//Set the Grid Page default page size
            grid.AllowPaging = allowpaging;//Set Paging for a grid as true or false
            //  grid.PagerStyle.PageSizeControlType = "RadDropDownList";
        

        }
        #endregion

        //This function is to set the visblity of Colum and Row total and Subtotal display
        #region TotalSettings
        public static void TotalSettings(RadPivotGrid grid, TotalsPosition ColumnGrandTotalsPosition, TotalsPosition ColumnsSubTotalsPosition, TotalsPosition RowGrandTotalsPosition, TotalsPosition RowsSubTotalsPosition)
        {
            grid.TotalsSettings.ColumnGrandTotalsPosition = ColumnGrandTotalsPosition;
            grid.TotalsSettings.ColumnsSubTotalsPosition = ColumnsSubTotalsPosition;
            grid.TotalsSettings.RowGrandTotalsPosition = RowGrandTotalsPosition;
            grid.TotalsSettings.RowsSubTotalsPosition=RowsSubTotalsPosition;
        

        }
        #endregion

        // bind the Datatable to  grid
        #region DataBind
        public static void DataBinds(RadPivotGrid grid, DataTable dataTable, Boolean needdatasource)
        {

            grid.DataSource = dataTable;
            if (!needdatasource)
            {
                grid.DataBind();
            }
        }
        public static void DataBinds(RadPivotGrid grid, DataSet dataSet, Boolean needdatasource)
        {
            DataBinds(grid, dataSet.Tables[0], needdatasource);
        }
        #endregion

        //here we define each column type and value and text
        #region PivotgridColumnType

        //To set the Grid header column groups
        #region PivotGridRowFiled
        public static void PivotGridRowFiled(RadPivotGrid grid,  String datafield, String UniqueName, int Width,TelerikControlType Columntype,String contolID)
        {
            PivotGridRowField rowField = new PivotGridRowField();
            if(Columntype!=TelerikControlType.None)
            {
            rowField.CellTemplate = new MyTemplate(datafield, Columntype.ToString(), Width - 2, contolID);
            }
            rowField.DataField = datafield;
            rowField.UniqueName = UniqueName;
            rowField.CellStyle.Width = Width;
            grid.Fields.Add(rowField);

        }
        #endregion

         #region PivotGridColumnFiled
        public static void PivotGridColumnFiled(RadPivotGrid grid,  String datafield, String UniqueName, int Width)
        {
            PivotGridColumnField  columnField = new PivotGridColumnField ();
           // rowField.CellTemplate=
            columnField.DataField = datafield;
            columnField.UniqueName = UniqueName;
            columnField.CellStyle.Width = Width;

            grid.Fields.Add(columnField);

        }
        #endregion

            #region PivotGridAggregateFiled
        public static void PivotGridAggregateFiled(RadPivotGrid grid,  String datafield, String UniqueName, int Width,String DataFormatString,String totalformatString)
        {
            PivotGridColumnField  aggregateField = new PivotGridColumnField ();
           // rowField.CellTemplate=
            aggregateField.DataField = datafield;
            aggregateField.UniqueName = UniqueName;
            aggregateField.CellStyle.Width = Width;
             aggregateField.DataFormatString = DataFormatString;
                aggregateField.TotalFormatString = totalformatString;
                grid.Fields.Add(aggregateField);

        }
        #endregion

        #endregion



    now everything looks good but i need to create a cell template which has Lable,Div controls need to add in aggregate and in row Column.

    is there any sample for creating cell template for Pivot grid by program.
  2. Kostadin
    Admin
    Kostadin avatar
    1708 posts

    Posted 17 Oct 2013 Link to this post

    Hi Syed,

    When creating template columns programmatically, the pivot grid must be generated completely in the code-behind using the Page_Init event and you must define a custom class that implements the ITemplate interface. We do not have such example but you could check out the following RadGRid's help article which demonstrates how to create a template column. The approach is similar with the difference that you have to create a CellTemplate.

    Regards,
    Kostadin
    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Syed
    Syed avatar
    36 posts
    Member since:
    Sep 2013

    Posted 17 Oct 2013 Link to this post

    Hi,
    Thanks for your reply,
    I fallow the same was as radgrid template class creation.

    For pivot grid i have createded a helper class and also i have created all pivotgrid in Init function.
    everything is working just i want to know how to bind the data to lable or textbox.
    in Rad grid template class we bind textbox or Lable like this.

     public void textBox_DataBinding(object sender, EventArgs e)
            {
                TextBox txt = (TextBox)sender;
                GridDataItem container = (GridDataItem)txt.NamingContainer;
                txt.Text = ((DataRowView)container.DataItem)[colname].ToString();
            }

    I want an example how to bind for the Pivot grid using template class.

    here is my Pivot grid helper class :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data;
    using Telerik.Web.UI;
    using System.Drawing;
    using System.Web.UI.WebControls;
    using System.Web.UI;
    using eis = Telerik.Web.UI.ExportInfrastructure;
    /// <summary>
    /// Author      : Shanu
    /// Create date : 2013-10-08
    /// Description : telerikPivotGridHelper
    /// Latest
    /// Modifier    : Shanu
    /// Modify date : 2013-10-08
    /// </summary>
    public class TelerikPivotGridHelper
    {
     public TelerikPivotGridHelper()
     {}

        //Set all the telerik Grid layout
        #region Layout
        public static void Layouts(RadPivotGrid grid, int height, int width, Boolean widthtype, Boolean allowsorting, Boolean ShowRowHeaderZone, Boolean ShowDataHeaderZone, Boolean ShowColumnHeaderZone)
        {
       
            grid.Skin = "Office2007";// set the Skin

            grid.EmptyValue = "";     
              
            grid.Height = height; //Set the height of the grid  in % or in pixcel
            if (width > 0)
            {
                if (widthtype == false)
                {
                    grid.Width = width; // set the Width of the grid  in % or in pixcel
                }
                else
                {
                    grid.Width = Unit.Percentage(width);
                }
            }
            grid.AllowSorting = allowsorting;
            grid.AllowFiltering = false;
            grid.ShowFilterHeaderZone = false;
            grid.ShowDataHeaderZone = ShowDataHeaderZone;
            grid.ShowFilterHeaderZone = false;
            grid.ShowColumnHeaderZone = ShowColumnHeaderZone;
            grid.ShowRowHeaderZone = ShowRowHeaderZone;
            grid.ClientSettings.Scrolling.AllowVerticalScroll = true;
            grid.ClientSettings.Scrolling.SaveScrollPosition = true;

        }

        #endregion

        //Set all the telerik Grid Page
        #region LayoutPage
        public static void LayoutPage(RadPivotGrid grid, int pagesize, Boolean allowpaging)
        {
            grid.PageSize = pagesize;//Set the Grid Page default page size
            grid.AllowPaging = allowpaging;//Set Paging for a grid as true or false
            //  grid.PagerStyle.PageSizeControlType = "RadDropDownList";
        

        }
        #endregion

        //This function is to set the visblity of Colum and Row total and Subtotal display
        #region TotalSettings
        public static void TotalSettings(RadPivotGrid grid, TotalsPosition ColumnGrandTotalsPosition, TotalsPosition ColumnsSubTotalsPosition, TotalsPosition RowGrandTotalsPosition, TotalsPosition RowsSubTotalsPosition)
        {
            grid.TotalsSettings.ColumnGrandTotalsPosition = ColumnGrandTotalsPosition;
            grid.TotalsSettings.ColumnsSubTotalsPosition = ColumnsSubTotalsPosition;
            grid.TotalsSettings.RowGrandTotalsPosition = RowGrandTotalsPosition;
            grid.TotalsSettings.RowsSubTotalsPosition=RowsSubTotalsPosition;
        

        }
        #endregion

        // bind the Datatable to  grid
        #region DataBind
        public static void DataBinds(RadPivotGrid grid, DataTable dataTable, Boolean needdatasource)
        {

            grid.DataSource = dataTable;
            if (!needdatasource)
            {
                grid.DataBind();
            }
        }
        public static void DataBinds(RadPivotGrid grid, DataSet dataSet, Boolean needdatasource)
        {
            DataBinds(grid, dataSet.Tables[0], needdatasource);
        }
        #endregion

        //here we define each column type and value and text
        #region PivotgridColumnType

        //To set the Grid header column groups
        #region PivotGridRowFiled
        public static void PivotGridRowFiled(RadPivotGrid grid,String headertext,  String datafield, String UniqueName, int Width,TelerikControlType Columntype,String contolID)
        {
            PivotGridRowField rowField = new PivotGridRowField();
            if (Columntype != TelerikControlType.None)
            {
                rowField.CellTemplate = new MyTemplate(datafield, Columntype.ToString(), Width - 2, contolID);
            }
            rowField.DataField = datafield;
            rowField.UniqueName = UniqueName;
            rowField.CellStyle.Width = Width;
            rowField.Caption = headertext;
            grid.Fields.Add(rowField);

        }
        #endregion

         #region PivotGridColumnFiled
        public static void PivotGridColumnFiled(RadPivotGrid grid, String headertext, String datafield, String UniqueName, int Width, TelerikControlType Columntype, String contolID)
        {
            PivotGridColumnField  columnField = new PivotGridColumnField ();
            if (Columntype != TelerikControlType.None)
            {
                columnField.CellTemplate = new MyTemplate(datafield, Columntype.ToString(), Width - 2, contolID);
            }
            columnField.DataField = datafield;
            columnField.UniqueName = UniqueName;
            if (Width > 0)
            {
                columnField.CellStyle.Width = Width;
            }
            columnField.Caption = headertext;
            grid.Fields.Add(columnField);

        }
        #endregion

            #region PivotGridAggregateFiled
        public static void PivotGridAggregateFiled(RadPivotGrid grid,String headertext, String datafield, String UniqueName, int Width, String DataFormatString, String totalformatString, TelerikControlType Columntype, String contolID)
        {
            PivotGridAggregateField aggregateField = new PivotGridAggregateField();
            if (Columntype != TelerikControlType.None)
            {
                aggregateField.CellTemplate = new MyTemplate(datafield, Columntype.ToString(), Width - 2, contolID);
            }
            aggregateField.DataField = datafield;
            aggregateField.UniqueName = UniqueName;
            aggregateField.CellStyle.Width = Width;
             aggregateField.DataFormatString = DataFormatString;
                aggregateField.TotalFormatString = totalformatString;
                aggregateField.Caption = headertext;
                grid.Fields.Add(aggregateField);

        }
        #endregion

        #endregion

        # region TemplateColumnClass
        public class MyTemplate : ITemplate
        {
            #region Variables

            // controls
             protected Label label;
            protected System.Web.UI.HtmlControls.HtmlGenericControl dynDiv;
           
            //loacl variable
            private String colname;
            private String Columntype;
            private int cntrlwidth;
            private String cntrlId;
         

            # endregion

            #region bindControls

            public MyTemplate(string cName, String Ctype, int controlWidth, String ControlID )
            {
                colname = cName;
                Columntype = Ctype;
                cntrlwidth = controlWidth;
                cntrlId = ControlID;
             
            }

            public void InstantiateIn(System.Web.UI.Control container)
            {
                switch (Columntype)
                {
                 

      case "Label":

                        label = new Label();
                        label.ID = cntrlId;
                        label.Width = cntrlwidth;
                        label.DataBinding += new EventHandler(label_DataBinding);

                        container.Controls.Add(label);
                        break;

                    case "DIV":

                        dynDiv = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
                        dynDiv.ID = cntrlId;
                        dynDiv.Attributes["style"]  = "float:left;margin: 0px -10px 0px -10px; padding: 0px;";
                       // dynDiv.Width = cntrlwidth;
                     
                       // label.DataBinding += new EventHandler(label_DataBinding);
                      //  label.ForeColor = Color.Red;
                        container.Controls.Add(dynDiv);
                        break;
                }
    //this is used in telerik grid how to use same thing for Pivot grid
      public void label_DataBinding(object sender, EventArgs e)
            {
                Label lbltxt = (Label)sender;
                GridDataItem container = (GridDataItem)lbltxt.NamingContainer;
                lbltxt.Text = ((DataRowView)container.DataItem)[colname].ToString();
            }
            }
         

            # endregion

        }
        # endregion
    }

    And in my CS Init method  i call this class like this

     protected void Page_Init(object source, System.EventArgs e)
        {
            InitializeGridControl();
        }

         protected void InitializeGridControl()
         {
             TelerikPivotGridHelper.Layouts(RadGrid1, 600, 99,true, false,true,false,false);
             TelerikPivotGridHelper.LayoutPage(RadGrid1, 50, true);
             TelerikPivotGridHelper.TotalSettings(RadGrid1, TotalsPosition.None, TotalsPosition.None, TotalsPosition.None, TotalsPosition.None);

             TelerikPivotGridHelper.PivotGridRowFiled(RadGrid1,"Custname", "CustName", "CustName", 100,TelerikControlType.None,"");
             TelerikPivotGridHelper.PivotGridRowFiled(RadGrid1,"ToolNo", "ToolNo", "ToolNo", 100, TelerikControlType.None, "");
             TelerikPivotGridHelper.PivotGridRowFiled(RadGrid1,"Partname", "partName", "partName", 100, TelerikControlType.None, "");
             TelerikPivotGridHelper.PivotGridRowFiled(RadGrid1,"Oper", "OPER", "OPER", 100, TelerikControlType.None, "");
             //TelerikPivotGridHelper.PivotGridRowFiled(RadGrid1,"OperDesc", "OperDesc", "OperDesc", 100, TelerikControlType.None, "");

             TelerikPivotGridHelper.PivotGridColumnFiled(RadGrid1,"Sc Date", "YDATE", "YDATE", 0, TelerikControlType.None, "");
             TelerikPivotGridHelper.PivotGridColumnFiled(RadGrid1, "MON", "ASDT", "ASDT", 0, TelerikControlType.None, "");
             TelerikPivotGridHelper.PivotGridColumnFiled(RadGrid1, "", "dt", "dt", 0, TelerikControlType.None, "");
             TelerikPivotGridHelper.PivotGridColumnFiled(RadGrid1, "", "dayname", "dayname", 0, TelerikControlType.None, "");

             TelerikPivotGridHelper.PivotGridAggregateFiled(RadGrid1, "", "rowsID", "rowsID", 28, "{0:F2}", "{0:F2}", TelerikControlType.DIV, "div1");

             RadGrid1.NeedDataSource +=new EventHandler<PivotGridNeedDataSourceEventArgs>(RadGrid1_NeedDataSource);
             RadGrid1.CellDataBound +=new EventHandler<PivotGridCellDataBoundEventArgs>(RadGrid1_CellDataBound);

         }

  5. Kostadin
    Admin
    Kostadin avatar
    1708 posts

    Posted 21 Oct 2013 Link to this post

    Hi Syed,

    You could use the same approach but instead getting the GridDataItem you have to get the PivotGridDataCell if you add the template to an Aggregate field. Check out the following code snippet.
    Label lbltxt = (Label)sender;
    PivotGridDataCell container = (PivotGridDataCell)lbltxt.NamingContainer;
    lbltxt.Text = container .DataItem.ToString();

    Note that by getting the NamingContainer you have to cast it to the appropriate type. This could be PivotGridDataCell, PivotGridRowHeaderCell or PivotGridColumnHeaderCell.

    I hope this information helps.

    Regards,
    Kostadin
    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.
  6. Syed
    Syed avatar
    36 posts
    Member since:
    Sep 2013

    Posted 21 Oct 2013 Link to this post

    Hi,
    Thanks
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017