Telerik RADGrid 和排序

发布于 2024-12-07 11:12:28 字数 1842 浏览 1 评论 0原文

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

由于某种原因,网格无法排序。这是我到目前为止所拥有的。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄居人 2024-12-14 11:12:29

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文