【问题标题】:WPF Datagrid Grouping , displaying autogenrated columnsWPF Datagrid Grouping ,显示自动生成的列
【发布时间】:2017-06-11 05:57:41
【问题描述】:

我正在开发一个绑定到以下类集合的 wpf 数据网格

public class SalesData
{
    public string SalesManName{ get; set; }
    public int salesCount{ get; set; }
    public string Area { get; set; }
    public datetime salesdate{get;set;}            
    public int avgSales{ get; set; }
}

在我的 UI 中单击一个按钮,我正在执行以下操作

CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(Datagrid.ItemsSource);
view.SortDescriptions.Add(new SortDescription("SalesCount", ListSortDirection.Descending));

PropertyGroupDescription groupDescription = new PropertyGroupDescription("SalesManName");

PropertyGroupDescription groupDescription2 = new PropertyGroupDescription("salesdate");

现在如果基本上发生的事情是它在数据网格中给我以下结果

SalesManName | SalesCount | Area   | SalesDate  
AAA          | 2          | London | 05/06/2017 
AAA          | 1          | London | 05/07/2017 
AAA          | 1          | London | 05/08/2017 
AAA          | 2          | London | 05/09/2017 
BBB          | 2          | London | 05/06/2017 
BBB          | 2          | London | 05/07/2017 
BBB          | 2          | London | 05/09/2017 

我想要的是

SalesManName | 05/06/2017  | 05/07/2017  | 05/08/2017  | 05/09/2017 
AAA          | 2           |     1       |     1       |   2
BBB          | 2           |     2       |     0       |   2

【问题讨论】:

  • 1) 在view 的GroupDescriptions 中添加您添加组描述的代码。 2).GetDefaultView(Datagrid.ItemsSource)你是认真的吗?您需要在视图模型中创建集合,将其包装到 ICollectionView 并将 DataGrid 的 ItemsSource 绑定到此集合视图。
  • PropertyGroupDescription 不是用来旋转数据的;如果您使用的是 MVVM,您可以自己在新数据结构中旋转您的 SalesData 集合并将其绑定到您的 DataGrid。

标签: wpf wpfdatagrid


【解决方案1】:

很久以前我也做过同样的事情。不幸的是,我不记得帮助我的帖子了。这个想法包括使用DataTable 作为ItemsSource 的DataGrid 以及将构建数据表的转换器。 Xaml

<DataGrid GridLinesVisibility="None" ItemsSource="{Binding AllItems, Converter={StaticResource SalesDataConverter}, Mode=OneWay}" IsReadOnly="True" />

其中AllItems 是ObservableCollection 的SalesData

转换器

public class SalesDataConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var dataTable = new DataTable();
        const string format = "MM/dd/yyyy";

        var allSalesData = value as IEnumerable<SalesData>;
        var headers = allSalesData.Select(s => s.SalesDate.ToString(format)).Distinct();

        dataTable.Columns.Add("SalesManName");
        foreach (var header in headers)
        {
            dataTable.Columns.Add(new DataColumn(header, typeof(string)));
        }

        var saleDico = allSalesData.GroupBy(s => s.SalesManName)
            .Select(g => new
            {
                SalesManName = g.Key,
                DateAndCount = g.ToList().GroupBy(i => i.SalesDate.ToString(format))
                .ToDictionary(e => e.Key, e => e.ToList().Sum(f => f.SalesCount))
            });

        foreach (var item in saleDico)
        {
            var values = new List<string>() { item.SalesManName };
            int count;
            foreach (var h in headers)
            {
                var val = item.DateAndCount.TryGetValue(h, out count) ? count.ToString() : string.Empty;
                values.Add(val);           
            }
            dataTable.Rows.Add(values.ToArray());
        }
        return dataTable.AsDataView();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

【讨论】:

  • 感谢@adPartage:您的意见很有帮助。接受这是实现我所需要的一种可能的方式。非常感谢!
猜你喜欢
  • 2014-03-12
  • 2014-06-06
  • 2013-12-25
  • 2012-04-24
  • 1970-01-01
  • 2017-10-23
  • 1970-01-01
  • 2018-12-06
  • 1970-01-01
相关资源
最近更新 更多