Is it possible to add RadSpinElement inside GridView column header ?

8 posts, 0 answers
  1. miki
    miki avatar
    37 posts
    Member since:
    Dec 2014

    Posted 15 Jan 2015 Link to this post

    Not inside each Row Cell.

    Thanks
  2. Dimitar
    Admin
    Dimitar avatar
    1404 posts

    Posted 20 Jan 2015 Link to this post

    Hi Miki,

    Thank you for writing.

    This is possible and to achieve it you should create a custom cell element:
    class MyGridHeaderCellElement : GridHeaderCellElement
    {
        public MyGridHeaderCellElement(GridViewColumn col, GridRowElement row) : base(col,row)
        {
        }
     
        protected override void CreateChildElements()
        {
            base.CreateChildElements();
            RadSpinElement element = new RadSpinElement();
           
            element.StretchVertically = false;
          
            this.Children.Add(element);
        }
     
        protected override Type ThemeEffectiveType
        {
            get
            {
                return typeof(GridHeaderCellElement);
            }
        }
         
    }

    The default cell can be changed like this:
    void radGridView1_CreateCell(object sender, Telerik.WinControls.UI.GridViewCreateCellEventArgs e)
    {
        if (e.CellType == typeof(GridHeaderCellElement))
        {
            e.CellElement = new MyGridHeaderCellElement(e.Column,e.Row);
        }
    }

    Additionally, you should create custom header row behavior and override the OnMouseDown method. This is necessary because by default the header cell click sorts the grid:
    class MyGridHeaderRowBehavior : GridHeaderRowBehavior
    {
        public override bool OnMouseDown(MouseEventArgs e)
        {
            MyGridHeaderCellElement element = this.GridControl.ElementTree.GetElementAtPoint(e.Location) as MyGridHeaderCellElement;
     
            if (element != null)
            {
                return base.OnMouseDown(e);
            }
            return true;
        }
    }

    You can change the default behavior with the following code:
    BaseGridBehavior gridBehavior = radGridView1.GridBehavior as BaseGridBehavior;
    gridBehavior.UnregisterBehavior(typeof(GridViewTableHeaderRowInfo));
    gridBehavior.RegisterBehavior(typeof(GridViewTableHeaderRowInfo), new MyGridHeaderRowBehavior());

    Please let me know if there is something else I can help you with. 
     
    Regards,
    Dimitar
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for WinForms is Visual Studio 2017 Ready
  4. miki
    miki avatar
    37 posts
    Member since:
    Dec 2014

    Posted 21 Jan 2015 in reply to Dimitar Link to this post

    The Spineditor that i want to add is inside my column header called Speed, so i change the radGridView1_CreateCell into this:

    private void radGridView1_CreateCell(object sender, GridViewCreateCellEventArgs e)
    {
        if (e.Column.HeaderText == "Speed" &&  e.CellType == typeof(GridHeaderCellElement))
        {
            e.CellElement = new MyGridHeaderCellElement(e.Column, e.Row);
        }
    }

    And now i can see the Spineditor only inside this column header.
    1. How can i see the header name "Speed" beside the Spineditor ?
    2. How can i set minimum and maximum values into my Spineditor  ?
    3. when i change the Spineditor value how can i catch this evnt in order to change my GridView objects accordingly ? 
  5. miki
    miki avatar
    37 posts
    Member since:
    Dec 2014

    Posted 21 Jan 2015 in reply to miki Link to this post

    This is my update for question number 3:
    I try to catch the Speneditor event inside Cell Click event:

            
    private void radGridView1_CellClick(object sender, GridViewCellEventArgs e)
            {
                string cellName = e.Column.HeaderText;
                if (e.RowIndex == -1 && cellName == "Speed")
                {
     
                }
            }
    Now how can i catch the Spineditor value after changed ?
  6. Dimitar
    Admin
    Dimitar avatar
    1404 posts

    Posted 22 Jan 2015 Link to this post

    Hi Miki,

    Thank you for writing back.

    1. You need to set the StretchHorizontallyAlignment and FitToSizeMode properties of the spin element.

    2. You can use the MinValue and MaxValue properties of the spin element.

    3.  The CellClick event is not suitable for this case. You should use the ValueChanged event of the spin element. You can subscribe to this event in the MyGridHeaderCellElement class, you can access the grid there as well.

    I have attached a small sample to show you how this can be implemented.

    Do not hesitate to contact us if you have other questions.
     
    Regards,
    Dimitar
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  7. miki
    miki avatar
    37 posts
    Member since:
    Dec 2014

    Posted 24 Jan 2015 in reply to Dimitar Link to this post

    Hi,

    When i am using your solution with a custom cell element i notice a strange behavior:

    Every time I try to reduce or expand cell header, even if i didn't release the left mouse button this cell automatically expanded and i cannot reduce it's size, the minimum size i can reduce is the original size, this cause in every cell header and this behavior was not previously.
  8. Dimitar
    Admin
    Dimitar avatar
    1404 posts

    Posted 28 Jan 2015 Link to this post

    Hello Miki,

    Thank you for writing back.

    This issue comes from the custom implementation in the OnMouseDown method. In this case I have not considered that this method will be triggered when the cells are resized. Nevertheless you can use the following implementation and this would not occur:
    class MyGridHeaderRowBehavior : GridHeaderRowBehavior
    {
        public override bool OnMouseDown(MouseEventArgs e)
        {
            RadElement element = this.GridControl.ElementTree.GetElementAtPoint(e.Location);
            if (element != null && element.FindAncestor<RadSpinElement>() != null)
            {
                return true;
            }
     
            return base.OnMouseDown(e);
        }
    }

    Hope I could help.
     
    Regards,
    Dimitar
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  9. miki
    miki avatar
    37 posts
    Member since:
    Dec 2014

    Posted 29 Jan 2015 in reply to Dimitar Link to this post

    That's work perfect !
    Thanks a lot for your help.
Back to Top
UI for WinForms is Visual Studio 2017 Ready