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

Extending the selection rectangle to cover the row selector cells

3 Answers 52 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Terry
Top achievements
Rank 1
Terry asked on 10 Jan 2012, 12:11 PM
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

3 Answers, 1 is accepted

Sort by
0
Accepted
Jack
Telerik team
answered on 13 Jan 2012, 10:15 AM
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).

0
Terry
Top achievements
Rank 1
answered on 13 Jan 2012, 11:47 AM
Wow, thanks. You went to an awful lot of trouble there, very much appreciated.

Terry
0
Jack
Telerik team
answered on 17 Jan 2012, 11:48 AM
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).

Tags
GridView
Asked by
Terry
Top achievements
Rank 1
Answers by
Jack
Telerik team
Terry
Top achievements
Rank 1
Share this question
or