【问题标题】:Errors trying to Sort a datagrid (XAML / C#)尝试对数据网格进行排序时出错 (XAML / C#)
【发布时间】:2013-04-10 01:30:21
【问题描述】:

应该是一个简单的问题,但我找不到解决办法。我在我的 c# 中收到这些错误:

“System.Windows.Controls.DataGrid”不包含“Sort”的定义,并且找不到接受“System.Windows.Controls.DataGrid”类型的第一个参数的扩展方法“Sort”(您是否缺少using 指令还是程序集引用?)

“System.Windows.Controls.DataGrid”不包含“Ascending”的定义,并且找不到接受“System.Windows.Controls.DataGrid”类型的第一个参数的扩展方法“Ascending”(您是否缺少using 指令还是程序集引用?)

这是我的代码:

XAML:

<DataGrid Grid.Row="0" Grid.Column="0" Grid.RowSpan="5" Margin="0 0 25 0"  AutoGenerateColumns="True" Name="studentData" IsReadOnly="True" ItemsSource="{Binding}" />

C#:

private void sortByName_Click(object sender, RoutedEventArgs e)
    {
        studentDataGrid.ItemsSource = studentClass;
        studentDataGrid.Sort(studentDataGrid.Columns[0], studentDataGrid.Ascending);
    }

我做错了什么?

提前致谢。

【问题讨论】:

  • 您有任何可以为数据网格显示的 xaml 吗?
  • 抱歉是要补充&lt;DataGrid Grid.Row="0" Grid.Column="0" Grid.RowSpan="5" Margin="0 0 25 0" AutoGenerateColumns="True" Name="studentData" IsReadOnly="True" ItemsSource="{Binding}" /&gt;

标签: c# datagrid


【解决方案1】:

DataGrid 没有 Sort 方法。 arelot 的博客文章和文章介绍了如何在代码中对数据网格应用排序。

【讨论】:

  • 谢谢,虽然我还有 1 个问题...如何创建 ListSortDirection?我正在尝试ListSortDirection direction = Ascending;,但它说The name 'Ascending' does not exist in the current context
  • 我是Enumeration ListSortDirection direction = ListSortDirection.Ascending;
猜你喜欢
  • 2021-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-25
  • 2014-09-04
  • 1970-01-01
  • 2021-12-17
相关资源
最近更新 更多