2

I'm using the RADGridView for WPF to display some data. It is pulled dynamically from DB so I don't know column names or the type of data contained in each cell. I want to let the user sort the data on each column when it double-clicks on a column header.

For some reason the grid doesn't sort. This is what I have so far.

private void SetEventHandlers()
        {
            if (_grid != null)
            {
                _grid.AddHandler(GridViewCellBase.CellDoubleClickEvent, new EventHandler<RadRoutedEventArgs>(OnCellDoubleClick), true);

            }
        }


private void OnCellDoubleClick(object sender, RoutedEventArgs e)
        {
            GridViewCellBase cell = e.OriginalSource as GridViewCellBase;
            if (cell != null && cell is GridViewHeaderCell)
            {
                SetSorting(cell);
            }
        }



private void SetSorting(GridViewCellBase cell)
        {
            GridViewColumn column = cell.Column;
            SortingState nextState = GetNextSortingState(column.SortingState);
            _grid.SortDescriptors.Clear();
            if (nextState == SortingState.None)
            {
                column.SortingState = SortingState.None;
            }
            else
            {
                _grid.SortDescriptors.Add(CreateColumnDescriptor(column, nextState));
                column.SortingState = nextState;
            }

        }

EDIT:

private ColumnSortDescriptor CreateColumnDescriptor(GridViewColumn column, SortingState sortingState)
        {
            ColumnSortDescriptor descriptor = new ColumnSortDescriptor();
            descriptor.Column = column;
            if (sortingState == SortingState.Ascending)
            {
                descriptor.SortDirection = ListSortDirection.Ascending;
            }
            else
            {
                descriptor.SortDirection = ListSortDirection.Descending;
            }


            return descriptor;
        }
Carlos Blanco
  • 8,592
  • 17
  • 71
  • 101
  • 1
    Are you explicitly disabling the built-in sorting capability? By default, I believe clicking the column header will perform sorting (so trying to do sorting on double click sounds dangerous). Also, is there a reason you can't use the built-in sorting? But doing it the way you have should work. I was able to get a simple test going. I think you'll want to comment out the lines that set column.SortingState. That shouldn't be necessary. Can you post your `CreateColumnDescriptor` method? – Stephen McDaniel Oct 01 '11 at 08:29
  • I posted 'CreateColumnDescriptor' code. The reason we disabled the default sorting is because we are sharing "Views" that have sorting states and storing them in the database. Sounds weird I know – Carlos Blanco Oct 03 '11 at 14:40
  • 1
    The code you posted seems to work fine when I try it. What behavior are you getting? Have you tried putting breakpoints in the various places to make sure things are happening as you are expecting? I assume you are calling `SetEventHandlers` at the appropriate time (it seems weird that you check for `null` - if the grid is null, then nothing will happen because you won't be able to attach the events)? – Stephen McDaniel Oct 04 '11 at 05:08
  • I found what the issue is. The Grid is bound to a collection of objects. In order to sort its elements I need to sort this collection. – Carlos Blanco Oct 04 '11 at 14:01
  • You should post what you did to solve the problem in an answer and mark it as accepted (that is much easier for people to see in the future instead of just these comments). – Stephen McDaniel Oct 07 '11 at 06:12

1 Answers1

1

It turned out than my RadGrid data was binded to an ObservableCollection. Sorting functionality of the grid itself did not work. Sorting the ObservableCollection was the solution. I ended up sorting the ObservableCollection using linq.

m.s.
  • 16,063
  • 7
  • 53
  • 88
Carlos Blanco
  • 8,592
  • 17
  • 71
  • 101