2

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

1 に答える 1

1

私のRadGridデータがObservableCollectionにバインドされていたことが判明しました。グリッド自体のソート機能が機能しませんでした。ObservableCollection をソートすることが解決策でした。linqを使用してObservableCollectionをソートすることになりました。

于 2011-10-07T16:00:32.157 に答える