WPF Datagrid的组和排序 [英] WPF Datagrid group and sort

查看:587
本文介绍了WPF Datagrid的组和排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实施WPF数据网格分组。我想分组的项目进行排序。
为例DataGrid的是有四列(EMPNO,姓名,部门,地址)。我被dept列做分组。
当我点击dept列标题我想分组的项目进行排序。

I am implementing grouping in WPF datagrid. I want to sort the grouped items. For example datagrid is having four columns(empno,name,dept,address). I am doing grouping by dept column. when I click on the dept column header I want to sort the grouped items.

下面我使用的ListCollectionView到组中的代码的项目后面。

Here I am using ListCollectionView to group the items in the code behind.

public  ListCollectionView collection;
collection = new ListCollectionView(obj.empData);

collection.GroupDescriptions.Add(new PropertyGroupDescription("Country"));
dgData.Items.SortDescriptions.Add
          (new System.ComponentModel.SortDescription
                   ("Country"
                    ,System.ComponentModel.ListSortDirection.Descending
                   )
           );
dgData.Items.SortDescriptions.Add
          (new System.ComponentModel.SortDescription
                    ("Contact"
                     , System.ComponentModel.ListSortDirection.Descending
                    )
          );
dgData.ItemsSource = collection;

private void dgData_Sorting
        (object sender, Microsoft.Windows.Controls.DataGridSortingEventArgs e)
{
    if (e.Column.SortDirection.ToString() == "Ascending")
    {
        //dgData.Items.SortDescriptions.Clear();
        collection.Refresh();
        collection = new ListCollectionView(obj.empData);
        collection.GroupDescriptions.Add(new PropertyGroupDescription("Country"));
        dgData.Items.SortDescriptions.Add
             ( new System.ComponentModel.SortDescription
                  ("Country"
                   , System.ComponentModel.ListSortDirection.Descending
                  )
              );
         dgData.ItemsSource = collection;
    }
}



更​​改排序顺序之后,它不会在反射UI。请让我知道要实现这个正确的方法

After changing the sort order it is not reflecting in the UI. Please let me know the correct way to implement this.

推荐答案

你见过MSDN文章的如何:排序的GridView列当一个头被点击是指从的ListView样的排序数据样本和后者(的下载示例)链接

Have you seen the MSDN article How to: Sort a GridView Column When a Header Is Clicked which refers to the sample from ListView That Sorts Data Sample and the latter has (Download sample) link

有趣,但最终参考的样本下载只能通过.NET 3.0和3.5版本的MSDN文章,但不是通过版本.NET 4.0和4.5尽管代码段是相同的。

Funny but the eventual reference to sample download is available only through .NET 3.0 and 3.5 version of the MSDN article but not through versions for .NET 4.0 and 4.5 though the code snippets are the same.

有也高于MSDN示例基于与样品沼泽文章:

There are also bog articles with samples based on above MSDN sample:

  • Thomas Levesque's [WPF] Automatically sort a GridView when a column header is clicked
  • Thomas Levesque's [WPF] Automatically sort a GridView (continued)
    The latter also has a code

有也有可运行Visual Studio项目MSDN博客文章(与WPF工具包depemnency):

There is also MSDN blog articles with runnable Visual Studio project (with depemnency on WPF Toolkit) :

  • WPF DataGrid: Tri-state Sorting sample
  • Improving Microsoft DataGrid CTP sorting performance

这篇关于WPF Datagrid的组和排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆