Custom GridDataCellElement for RadGridView. Adding logic to CreateChildElements

4 posts, 1 answers
  1. Tim
    Tim avatar
    4 posts
    Member since:
    Apr 2012

    Posted 19 Apr 2012 Link to this post

    Hey Guys

    I have a problem where i wish to add logic into the CreateChildElements method which, based on the value of a given cell in the row will display different controls.

    When CreateChildElements is called, the GridViewColumn and GridRowElement (which have been set in the contructor) are Null, as if they are not in the same context.

    This is what i currently have (Please notice the CreateChildElements Method)
    #region GridDataCellElement
     public class GridDataCustomCellElement : GridDataCellElement
     {
         #region Locals
         private RadImageButtonElement radButtonElementViewOrder;
         private RadImageButtonElement radButtonElementNewOrder;
         public GridViewColumn thisColumn;
         public GridRowElement thisRow;
         #endregion
     
         #region Constructors
          
         public GridDataCustomCellElement(GridViewColumn col, GridRowElement row) : base(col, row) { thisColumn = col; thisRow = row;}
         #endregion
     
         #region Events
     
         void radButtonElementNewOrder_Click(object sender, EventArgs e)
         {
     
             Main newOrder = new Main(GetFolderPath(thisRow.GridControl.CurrentRow.Cells[2].Value.ToString()) + "\\" + thisRow.GridControl.CurrentRow.Cells[0].Value.ToString());
             newOrder.MyParentForm = (SalesOrderingClient.Home)thisRow.GridControl.FindForm();
             newOrder.IsCopyOrder = true;
             newOrder.ShowDialog();
         }
     
         void radButtonElementViewOrder_Click(object sender, EventArgs e)
         {
             // Build order object
             SalesOrderingObjects.Order objOrder = SalesOrderingObjects.Order.LoadOrder(GetFolderPath(thisRow.GridControl.CurrentRow.Cells[2].Value.ToString()) + "\\" + thisRow.GridControl.CurrentRow.Cells[0].Value.ToString());
     
             // Build session object
             Session s = new Session(objOrder);
     
             // Display Order
             Order o = new Order(s);
             o.ShowDialog();
         }
     
         #endregion
     
         #region Overrides
          
         /// <summary>
         /// Create new controls and add them to cell
         /// </summary>
         protected override void CreateChildElements()
         {
             // Set up controls and add them to this cell
     
             Home h = new Home();
              
     
             radButtonElementViewOrder = new RadImageButtonElement();
             radButtonElementViewOrder.ButtonFillElement.BackColor = Color.Transparent;
             radButtonElementViewOrder.ButtonFillElement.BackColor2 = Color.Transparent;
             radButtonElementViewOrder.ImageAlignment = ContentAlignment.MiddleRight;
             radButtonElementViewOrder.BorderElement.Width = 0;
             radButtonElementViewOrder.Image = h.IconImageLIst.Images[1];
             radButtonElementViewOrder.ToolTipText = "View Order";
              
             this.Children.Add(radButtonElementViewOrder);
     
             radButtonElementNewOrder = new RadImageButtonElement();
             radButtonElementNewOrder.ButtonFillElement.BackColor = Color.Transparent;
             radButtonElementNewOrder.ButtonFillElement.BackColor2 = Color.Transparent;
             radButtonElementNewOrder.ImageAlignment = ContentAlignment.MiddleRight;
             radButtonElementNewOrder.BorderElement.Width = 0;
             radButtonElementNewOrder.Image = h.IconImageLIst.Images[0];
             radButtonElementNewOrder.ToolTipText = "Copy Order";
             this.Children.Add(radButtonElementNewOrder);
     
             radButtonElementNewOrder.Click += new EventHandler(radButtonElementNewOrder_Click);
             radButtonElementViewOrder.Click += new EventHandler(radButtonElementViewOrder_Click);
         }
     
     
     
     
     
         /// <summary>
         /// Set size and position of controls in the cell
         /// </summary>
         /// <param name="finalSize"></param>
         /// <returns></returns>
         protected override SizeF ArrangeOverride(SizeF finalSize)
         {
             // If there are more than 1 controls in this cell
             //if (this.Children.Count == 3)
             //{
                 // Setup dimensions and position and modifiy controls
             RectangleF btn1 = new RectangleF(20, 12, 36, 36);
             RectangleF btn2 = new RectangleF(58, 12, 36, 36);
     
              
             this.Children[0].Arrange(btn1);
             this.Children[1].Arrange(btn2);
             //}
     
             return finalSize;
         }
     
         /// <summary>
         /// Set the cells theme, if not done, cell does not inherit theme as it is custom
         /// </summary>
         protected override Type ThemeEffectiveType
         {
             get
             {
                 return typeof(GridDataCellElement);
             }
         }
     
         public override bool IsCompatible(GridViewColumn data, object context)
         {
             return data is RadLabelColumn && context is GridDataRowElement;
         }
     
         #endregion
     
         #region Private Methods
     
         /// <summary>
         /// Get folder path based on XML status
         /// </summary>
         /// <param name="status"></param>
         /// <returns></returns>
         string GetFolderPath(string status)
         {
             string folder = string.Empty;
             switch (status)
             {
                 case "Saved":
                     folder = Common.SavedOrdersFolder;
                     break;
                 case "InProgress":
                     folder = Common.InProgressOrdersFolder;
                     break;
                 case "Sent":
                     folder = Common.SentOrdersFolder;
                     break;
                 case "Requested":
                     folder = Common.RequestedOrdersFolder;
                     break;
                 default:
                     folder = Common.UserDataBaseFolder;
                     break;
             }
             return folder;
         }
     
         #endregion
     
     }
     #endregion

    This is what i would like to do:
    protected override void CreateChildElements()
    {
        // Set up controls and add them to this cell
     
        Home h = new Home();
         
        switch(SOMETHING GOES HERE)
        {
            case ?:
            radButtonElementViewOrder = new RadImageButtonElement();
            radButtonElementViewOrder.ButtonFillElement.BackColor = Color.Transparent;
            radButtonElementViewOrder.ButtonFillElement.BackColor2 = Color.Transparent;
            radButtonElementViewOrder.ImageAlignment = ContentAlignment.MiddleRight;
            radButtonElementViewOrder.BorderElement.Width = 0;
            radButtonElementViewOrder.Image = h.IconImageLIst.Images[1];
            radButtonElementViewOrder.ToolTipText = "View Order";
            this.Children.Add(radButtonElementViewOrder);
     
            radButtonElementNewOrder = new RadImageButtonElement();
            radButtonElementNewOrder.ButtonFillElement.BackColor = Color.Transparent;
            radButtonElementNewOrder.ButtonFillElement.BackColor2 = Color.Transparent;
            radButtonElementNewOrder.ImageAlignment = ContentAlignment.MiddleRight;
            radButtonElementNewOrder.BorderElement.Width = 0;
            radButtonElementNewOrder.Image = h.IconImageLIst.Images[0];
            radButtonElementNewOrder.ToolTipText = "Copy Order";
            this.Children.Add(radButtonElementNewOrder);
     
            radButtonElementNewOrder.Click += new EventHandler(radButtonElementNewOrder_Click);
            radButtonElementViewOrder.Click += new EventHandler(radButtonElementViewOrder_Click);
            break;
            case ?:
                // ADD SOME DIFFERENT CONTROLS
            break;
        }
     
     
    }

    Please tell me if this is possible, or if there is an alternative way of doing this?

    Many Thanks
    Tim





  2. Ivan Petrov
    Admin
    Ivan Petrov avatar
    705 posts

    Posted 23 Apr 2012 Link to this post

    Hi Tim,

    Thank you for writing.

    Actually, the lifecycle of the RadElements is as follows - first the InitializeFields() method is called, then CreateChildElements() and after that the Constructor is executed. This is possible due to the base constructor explicitly calling these methods. The idea behind these methods is the following, InitializeFields() is meant to initialize fields of the current class, setting them with the lowest value precedence (default). The CreateChildElements() is for constructing the sub element tree of the current element. Any fields set in the constructor would not have been initialized and will have null/default values in these methods. The method you should be using for showing/hiding the child elements is SetContent(). 

    I hope this will be useful for you. Should you have further questions, I would be glad to help.
     
    Greetings,
    Ivan Petrov
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
  3. Answer
    Tim
    Tim avatar
    4 posts
    Member since:
    Apr 2012

    Posted 23 Apr 2012 Link to this post

    Hi Ivan

    Thanks for the response. sometime after i posted i found the solution i was looking for, i am not sure if this is the best way to do it but it works for me. For all those out there who are experiencing the same difficulty, here is my solution.

    In CreateChildElements build and add all of the controls that you wish to add to the custom column.  Then using the CellFormatting event in the parent class you can show and hide the controls.  In my case i was using a RadPageView, based on the tab i was on i wanted my grid to show different buttons.

    Add cell children:
                /// <summary>
                /// Create new controls and add them to cell
                /// </summary>
                protected override void CreateChildElements()
                {
                    // Set up controls and add them to this cell
     
                    Home h = new Home();
                    Telerik.WinControls.RoundRectShape shape = new Telerik.WinControls.RoundRectShape();
                    shape.BottomLeftRounded = true;
                    shape.BottomRightRounded = true;
                    shape.TopLeftRounded = true;
                    shape.TopRightRounded = true;
     
                    radButtonElementViewOrder = new RadImageButtonElement();
                    radButtonElementViewOrder.ButtonFillElement.BackColor = Color.FromArgb(211, 226, 244);
                    radButtonElementViewOrder.ButtonFillElement.BackColor2 = Color.Transparent;
                    radButtonElementViewOrder.ImageAlignment = ContentAlignment.MiddleCenter;
                    radButtonElementViewOrder.BorderElement.Width = 0;
                    radButtonElementViewOrder.Image = h.IconImageLIst.Images[5];
                    radButtonElementViewOrder.ToolTipText = "View Order";
                    radButtonElementViewOrder.Text = "View";
                    radButtonElementViewOrder.Visibility = Telerik.WinControls.ElementVisibility.Hidden;
                    radButtonElementViewOrder.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;
                    radButtonElementViewOrder.Shape = shape;
                    this.Children.Add(radButtonElementViewOrder);
     
                    radButtonElementCopyOrder = new RadImageButtonElement();
                    radButtonElementCopyOrder.ButtonFillElement.BackColor = Color.FromArgb(211, 226, 244);
                    radButtonElementCopyOrder.ButtonFillElement.BackColor2 = Color.Transparent;
                    radButtonElementCopyOrder.ImageAlignment = ContentAlignment.MiddleCenter;
                    radButtonElementCopyOrder.BorderElement.Width = 0;
                    radButtonElementCopyOrder.Image = h.IconImageLIst.Images[0];
                    radButtonElementCopyOrder.ToolTipText = "Copy Order";
                    radButtonElementCopyOrder.Visibility = Telerik.WinControls.ElementVisibility.Hidden;
                    radButtonElementCopyOrder.Text = "Copy";
                    radButtonElementCopyOrder.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;
                    radButtonElementCopyOrder.Shape = shape;
                    this.Children.Add(radButtonElementCopyOrder);
     
                          // Blah Blah, it goes on for a while...

    in the CellFormatting event in the parent class show or hide controls based of logic

            private void rgvOrders_CellFormatting(object sender, CellFormattingEventArgs e)
            {
                if (e.CellElement.ColumnInfo.FieldName == "Actions")
                {
     
                    // Button Order in Cell
                    // ------------------------------------
                    // 0 - radButtonElementViewOrder
                    // 1 - radButtonElementNewOrder
                    // 2 - radButtonElementDeleteOrder
                    // 3 - radButtonElementSendOrder
                    // 4 - radButtonElementEditOrder
                    // 5 - radButtonElementEditBackToSavedOrder
                    // ------------------------------------
     
                    RectangleF rectButtonPostion1 = new RectangleF(20, 12, 36, 36);
                    RectangleF rectButtonPostion2 = new RectangleF(61, 12, 36, 36);
                    RectangleF rectButtonPostion3 = new RectangleF(102, 12, 36, 36);
                    RectangleF rectButtonPostion4 = new RectangleF(143, 12, 36, 36);
                    RectangleF rectButtonPostion5 = new RectangleF(184, 12, 36, 36);
     
                    switch (e.CellElement.RowInfo.Cells[2].Value.ToString())
                    {
                        case "Saved":
     
                            /* Delete
                             * View
                             * Edit
                             * Copy
                             * Send
                             */
                            e.CellElement.Children.ElementAt(2).Visibility = Telerik.WinControls.ElementVisibility.Visible;
                            e.CellElement.Children.ElementAt(2).Arrange(rectButtonPostion1);                            
                            e.CellElement.Children.ElementAt(0).Visibility = Telerik.WinControls.ElementVisibility.Visible;
                            e.CellElement.Children.ElementAt(0).Arrange(rectButtonPostion2);
                            e.CellElement.Children.ElementAt(4).Visibility = Telerik.WinControls.ElementVisibility.Visible;
                            e.CellElement.Children.ElementAt(4).Arrange(rectButtonPostion3);                            
                            e.CellElement.Children.ElementAt(1).Visibility = Telerik.WinControls.ElementVisibility.Visible;
                            e.CellElement.Children.ElementAt(1).Arrange(rectButtonPostion4);                            
                            e.CellElement.Children.ElementAt(3).Visibility = Telerik.WinControls.ElementVisibility.Visible;
                            e.CellElement.Children.ElementAt(3).Arrange(rectButtonPostion5);
                            break;
                        case "InProgress":
                            /*
                             * Delete
                             * View
                             * Edit back to Saved
                             * Copy
                             */
     
                            e.CellElement.Children.ElementAt(2).Visibility = Telerik.WinControls.ElementVisibility.Visible;
                            e.CellElement.Children.ElementAt(2).Arrange(rectButtonPostion1);
                            e.CellElement.Children.ElementAt(0).Visibility = Telerik.WinControls.ElementVisibility.Visible;
                            e.CellElement.Children.ElementAt(0).Arrange(rectButtonPostion2);
                            e.CellElement.Children.ElementAt(5).Visibility = Telerik.WinControls.ElementVisibility.Visible;
                            e.CellElement.Children.ElementAt(5).Arrange(rectButtonPostion3);
                            e.CellElement.Children.ElementAt(1).Visibility = Telerik.WinControls.ElementVisibility.Visible;
                            e.CellElement.Children.ElementAt(1).Arrange(rectButtonPostion4);
                            break;
     
                                    // and so on...


    I hope that this helps sombody out.  if anyone knows a better way to do this then please let me know :)

    Tim

  4. Ivan Petrov
    Admin
    Ivan Petrov avatar
    705 posts

    Posted 26 Apr 2012 Link to this post

    Hi Tim,

    Thank you for writing back.

    This is a perfectly valid solution for the case. Thank you for sharing it with the community. I have marked it as answer so others can find it easier.

    Should you have further questions, do not hesitate to write back.
     
    All the best,
    Ivan Petrov
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Back to Top