c# - 在 Telerik RadGrid for Winforms 中选择包括折叠组在内的多行

标签 c# winforms telerik selection radgrid

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

是否可以在运行时通过鼠标拖动矩形来选择这些行,折叠和展开? 如果是这样,我该如何启用此功能?

最佳答案

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

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());

关于c# - 在 Telerik RadGrid for Winforms 中选择包括折叠组在内的多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32051264/

相关文章:

c# - 从父级调用 mdi 子方法

c# - 获取mysql中两个日期之间的列出日期(不包括周末)

css - Kendo TabStrip 中的引导网格

c# - 根据选定的网格行项目检索隐藏对象属性 C#

javascript - 回发中的 ASP.NET 锁屏(母版页)

c# - 如何在 ASP.Net 中设计公共(public) API?

c# - 将通用对象集合传递给需要基类型集合的方法

c# - 如何在按钮控件上使用动画 GIF?

c# - 如何确定谁是对象的创建者

c# - 有什么理由在字段上使用非常简单的属性吗?