Extending the selection rectangle to cover the row selector cells

4 posts, 1 answers
  1. Terry
    Terry avatar
    6 posts
    Member since:
    Nov 2011

    Posted 10 Jan 2012 Link to this post

    Just switched from a Winforms DataGridView to a RadGridView on a client project and the customer has asked if they can have the Selection Rectangle (as shown here http://www.telerik.com/help/winforms/gridview-rows-selection-rectangle.html 
    ) enabled on the row selectors as well.

    Dragging anywhere in the cells starts the selection but attempting to drag over a row header does not continue it. They would like to be able to start selecting multiple rows by dragging from the row selectors (to the left of each row).

    The issue is that they are used to the way the old DataGridView works and would like it to work the same way. A minor issue yes, but users don't like change.

    Is there a hack so that I can enable the rectangle selection to show and start when a mouse click occurs on the row selector too?

    Thanks in advance

    Terry
  2. Answer
    Jack
    Admin
    Jack avatar
    2333 posts

    Posted 13 Jan 2012 Link to this post

    Hello Terry,

    Thank you for contacting us. Yes, it is possible to change the selection behavior of RadGridView. This can be done by replacing the default row behavior. Please consider the code snippet below:
    public class CustomRowBehavior : GridDataRowBehavior
    {
        Point mouseDownLocation;
     
        public override bool OnMouseDown(MouseEventArgs e)
        {
            mouseDownLocation = e.Location;
            return base.OnMouseDown(e);
        }
     
        protected override bool ResizeSelectionRectangle(GridCellElement currentCell, Point currentLocation)
        {
            GridCellElement mouseDownCell = this.GetCellAtPoint(this.mouseDownLocation);
            ScrollableRowsContainerElement scrollableRows = GetElementAtPoint<ScrollableRowsContainerElement>(this.GridViewElement.ElementTree, currentLocation);
     
            if (scrollableRows == null || !(mouseDownCell is GridDataCellElement || mouseDownCell is GridRowHeaderCellElement))
            {
                return false;
            }
     
            System.Reflection.PropertyInfo pi = typeof(RadGridViewElement).GetProperty("SelectionRectangle");
            pi.SetValue(this.GridViewElement, this.CreateSelectionRectangle(currentLocation), new object[] { });
            this.GridViewElement.Invalidate();
            return true;
        }
     
        private Rectangle CreateSelectionRectangle(Point currentMouseLocation)
        {
            int width = Math.Abs(this.mouseDownLocation.X - currentMouseLocation.X);
            int height = Math.Abs(this.mouseDownLocation.Y - currentMouseLocation.Y);
     
            if (width == 0)
            {
                width = 1;
            }
            if (height == 0)
            {
                height = 1;
            }
     
            int x = Math.Min(this.mouseDownLocation.X, currentMouseLocation.X);
            int y = Math.Min(this.mouseDownLocation.Y, currentMouseLocation.Y);
            return new Rectangle(x, y, width, height);
        }
     
        T GetElementAtPoint<T>(ComponentElementTree componentTree, Point point) where T : RadElement
        {
            RadElement elementUnderMouse = componentTree.GetElementAtPoint(point);
     
            while (elementUnderMouse != null)
            {
                T item = elementUnderMouse as T;
     
                if (item != null)
                {
                    return item;
                }
     
                elementUnderMouse = elementUnderMouse.Parent;
            }
     
            return null;
        }
    }

    Use the following code to replace the behavior:
    ((BaseGridBehavior)this.radGridView1.GridBehavior).UnregisterBehavior(typeof(GridViewDataRowInfo));
    ((BaseGridBehavior)this.radGridView1.GridBehavior).RegisterBehavior(typeof(GridViewDataRowInfo), new CustomRowBehavior());

    I hope this helps. If you need further assistance, I will be glad to help.
     
    Greetings,
    Jack
    the Telerik team

    SP1 of Q3’11 of RadControls for WinForms is available for download (see what's new).

  3. Terry
    Terry avatar
    6 posts
    Member since:
    Nov 2011

    Posted 13 Jan 2012 Link to this post

    Wow, thanks. You went to an awful lot of trouble there, very much appreciated.

    Terry
  4. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 17 Jan 2012 Link to this post

    Hi Terry,

    I am glad to hear that I could help. I know that this is a lot of code and we will try to make our API more flexible and convenient in future. 

    Should you have any further questions, we will be glad to help.
     
    All the best,
    Jack
    the Telerik team

    SP1 of Q3’11 of RadControls for WinForms is available for download (see what's new).

Back to Top