Devexpress MVC Gridview 基于选定行的自定义排序

Devexpress MVC Gridview custom sort based on selected rows

我想知道如何将我选择的行放在网格的顶部。很重要的是我用的是DevExpress Asp.Net MVC GridView.

我有以下内容,这是我将数十个非解决方案混搭在一起的最佳尝试。请注意评论:

settings.Columns.Add("customsort").Settings.SortMode =  
DevExpress.XtraGrid.ColumnSortMode.Custom;

settings.CustomColumnSort += (sender, e) => {

    if (e.Column.FieldName == "customsort")
    {
        //these following two lines are supposed to work according to the DX support team, but there is no "grid" object
        bool isRow1Selected = grid.Selection.IsRowSelectedByKey(e.GetRow1Value(grid.KeyFieldName));
        bool isRow2Selected = grid.Selection.IsRowSelectedByKey(e.GetRow2Value(grid.KeyFieldName));
    }

    e.Handled = isRow1Selected != isRow2Selected;
    if (e.Handled)
    {
        //I don't even know whether this is right
        e.Result = isRow1Selected ? 1 : -1;
    }
};

简而言之,我需要将选定的行放在最前面,但我不知道如何获取我正在比较的两行或两列的选定状态。

DevEx 版本为 15.1

更新:代码示例:

settings.Columns.Add(column =>
        {
            //column.FieldName = "customsort";
            column.FieldName = "customsort";
            column.Caption = "customsort";

            column.ColumnType = MVCxGridViewColumnType.Default;
            //column.UnboundType = DevExpress.Data.UnboundColumnType.Integer;
            column.Settings.SortMode = DevExpress.XtraGrid.ColumnSortMode.Custom;


        });



        settings.CustomColumnSort += (sender, e) =>
        {
            var grid = (MVCxGridView)sender;

            if (e.Column.FieldName == "customsort")
            {

                bool isRow1Selected = grid.Selection.IsRowSelectedByKey(e.GetRow1Value(grid.KeyFieldName));
                bool isRow2Selected = grid.Selection.IsRowSelectedByKey(e.GetRow2Value(grid.KeyFieldName));

                e.Result = isRow2Selected.CompareTo(isRow1Selected);
                e.Handled = true;
            }
        };

如果我单击 "customsort" 列,它会执行回发,但排序顺序不会改变。所以至少我有所进展,但我还没有完全到达那里。

您有几个选项可以解决您的特定问题。

您需要将 sender 对象转换为 MVCxGridView 才能访问您尝试使用的属性。

settings.CustomColumnSort += (sender, e) => {
    var grid = (MVCxGridView)sender;

    if (e.Column.FieldName == "customsort") {            
        bool isRow1Selected = grid.Selection.IsRowSelectedByKey(e.GetRow1Value(grid.KeyFieldName));
        bool isRow2Selected = grid.Selection.IsRowSelectedByKey(e.GetRow2Value(grid.KeyFieldName));

        e.Result = isRow2Selected.CompareTo(isRow1Selected);
        e.Handled = true;
    }
};

您可以完全放弃 grid 变量,只关注自定义列。

settings.CustomColumnSort += (sender, e) => {
    var columnName = "customsort";        
    if (e.Column.FieldName == columnName) {            
        var c1 = Convert.ToBoolean(e.GetRow1Value(columnName));
        var c2 = Convert.ToBoolean(e.GetRow2Value(columnName));
        e.Result = c2.CompareTo(c1);
        e.Handled = true;
    }
};

现在假设您的自定义排序列是一个 Boolean 类型的复选框。

最后,运行 Boolean 列您可以沿着最简单的路线走下去,并通过将排序设置为降序来将选中的列设置为显示在顶部。 (真 = 1,假 = 0)

settings.Columns.Add("ColumnName").SortOrder = DevExpress.Data.ColumnSortOrder.Descending;

以下资源可能有用。

看看这个 Row Selection

看看这个 Sorting

我向DevExpress 的支持团队提出了这个问题。他们的反馈是,就我而言,我使用数据库服务器模式的地方不支持自定义排序。因此,我的代码是正确的,因为如果不使用服务器模式,那将是如何实现自定义排序的。

希望这对某人有所帮助,并为他们节省一些调试时间。