Telerik RADGrid和排序

时间:2011-09-30 18:24:34

标签: telerik radgridview

我正在使用RADGridView for WPF来显示一些数据。它是从DB动态提取的,所以我不知道列名或每个单元格中包含的数据类型。我希望让用户在双击列标题时对每列的数据进行排序。

由于某种原因,网格不排序。这是我到目前为止所做的。

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

        }

编辑:

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

1 个答案:

答案 0 :(得分:1)

事实证明,我的RadGrid数据绑定到了ObservableCollection。网格本身的排序功能不起作用。对ObservableCollection进行排序是解决方案。我最终使用linq对ObservableCollection进行了排序。