Telerik RADGrid 和排序
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,我的 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.