2

I am trying to figure out how to bring my selected rows to the top of the grid. Very important is that I am using the DevExpress Asp.Net MVC GridView.

I have the following, which is my best attempt at mashing together literally dozens of non-solutions. Please note the comments:

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

In short, I need to put selected rows on top, but I don't know how to get the selected state of the two rows or columns I'm comparing.

DevEx version is 15.1

UPDATE: code sample:

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

If I click on the "customsort" column, it does perform a postback, but the sort order does not change. So at least I'm getting somewhere, but I'm not quite there yet.

Captain Kenpachi
  • 6,960
  • 7
  • 47
  • 68

2 Answers2

1

You have a few options available to solve your particular issue.

You need to cast the sender object to the MVCxGridView in order to get access to the properties you are trying to use.

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

You could forego the grid variable altogether and just focus on the custom column.

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

Now the assumption here is that your custom sort column is a Boolean type like a check box.

Finally, running with the Boolean column you could go down the simplest route and set checked columns to appear on top by setting sort to descending. (True = 1, False = 0)

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

The following resources could prove useful.

Take a look at this for Row Selection

Take a look at this for Sorting

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • Well, I can make my custom sort column anything really. It was just put in so I could manually call a Customrow.Sort() method which would sort by selected instead. Thanks for this. 200 internets for you. – Captain Kenpachi May 13 '16 at 10:02
  • UPDATE: while your code compiles just fine. The gridview doesn't actually sort on this column. – Captain Kenpachi May 13 '16 at 12:04
  • Update post with what you've tried and I'll take a look at it when I get back. Did you look at the links. Afk right now – Nkosi May 13 '16 at 12:47
  • Thanks. And yes, I did. I used the guidelines on those pages. – Captain Kenpachi May 13 '16 at 12:51
  • @JuannStrauss, Put a break point in the `CustomColumnSort` and step through. check that it is processing the sort. Did you try the second suggestion. – Nkosi May 13 '16 at 18:14
1

I put this question to DevExpress' support team. Their feedback was that in my case, where I was using Database Server Mode does not support custom sorting. So, my code is correct insofar as that's how one would implement custom sorting if one wasn't using server mode.

Hope this helps someone and saves them some hours of debugging.

Captain Kenpachi
  • 6,960
  • 7
  • 47
  • 68