Phat Phuc
Phat Phuc

Reputation: 107

Select multiple rows including collapsed groups in the Telerik RadGrid for Winforms

Given is a Telerik RadGrid for Winforms at runtime, with multiple columns and some rows representing a group, of which some are collapsed and some are expanded. Groups can be nested. Dragging a rectangle over collapsed groups does not seem to work to select rows.

Is it possible to select these rows, collapsed and expanded, with the mouse at runtime by dragging a rectangle? If so, how do I enable this feature?

Upvotes: 1

Views: 1130

Answers (1)

Ivan Petrov
Ivan Petrov

Reputation: 668

Mouse selection in RadGridView is handled in RowBehavior classes. The particular class you need is GridDataRowBehavior since selection is performed on data rows. So I have created a custom behavior class inheriting from the default behavior and executing some extra code after the default selection.

public class CustomGridDataRowBehavior : GridDataRowBehavior
{
    protected override bool ProcessMouseSelection(System.Drawing.Point mousePosition, GridCellElement currentCell)
    {
        bool result = base.ProcessMouseSelection(mousePosition, currentCell);

        if (result)
        {
            List<GridViewRowInfo> orderedRows = new List<GridViewRowInfo>();

            PrintGridTraverser traverser = new PrintGridTraverser(this.GridControl.MasterView);
            traverser.ProcessHiddenRows = true; 
            traverser.ProcessHierarchy = true;

            while (traverser.MoveNext())
            {
                orderedRows.Add(traverser.Current);
            }

            int minIndex = int.MaxValue;
            int maxIndex = int.MinValue;

            foreach (GridViewDataRowInfo selectedRow in this.GridControl.SelectedRows)
            {
                int rowIndex = orderedRows.IndexOf(selectedRow);

                if (rowIndex > maxIndex)
                {
                    maxIndex = rowIndex;
                }

                if (rowIndex < minIndex)
                {
                    minIndex = rowIndex;
                }
            }

            this.GridControl.ClearSelection();

            for (int i = minIndex; i <= maxIndex; i++)
            {
                if (!orderedRows[i].IsSelected)
                {
                    orderedRows[i].IsSelected = true;
                }
            }
        }

        return result;
    }
}

Now to use this behavior you need to unregister the default one and register this one. Here is how:

BaseGridBehavior behavior = this.radGridView1.GridBehavior as BaseGridBehavior;
behavior.UnregisterBehavior(typeof(GridViewDataRowInfo));
behavior.RegisterBehavior(typeof(GridViewDataRowInfo), new CustomGridDataRowBehavior());

Upvotes: 4

Related Questions