Select 多行,包括 Telerik RadGrid for Winforms 中的折叠组

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

Given 是运行时的 Telerik RadGrid for Winforms,多列和一些行代表一个组,其中一些是折叠的,一些是展开的。组可以嵌套。在折叠组上拖动矩形似乎不适用于 select 行。

是否可以 select 这些行,折叠和展开,在运行时用鼠标拖动一个矩形? 如果可以,如何启用此功能?

RadGridView 中的鼠标选择在 RowBehavior classes 中处理。您需要的特定 class 是 GridDataRowBehavior,因为选择是在数据行上执行的。所以我创建了一个自定义行为 class 继承默认行为并在默认选择后执行一些额外的代码。

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;
    }
}

现在要使用此行为,您需要注销默认行为并注册此行为。方法如下:

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