WPF 数据网格分组,显示自动生成的列
WPF Datagrid Grouping , displaying autogenrated columns
我正在开发一个绑定到以下集合的 wpf 数据网格 class
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
很久以前我也这样做过。不幸的是,我不记得对我有帮助的 post。这个想法包括使用 DataTable
作为 DataGrid
的 ItemsSource
和将构建数据表的转换器。
Xaml
<DataGrid GridLinesVisibility="None" ItemsSource="{Binding AllItems, Converter={StaticResource SalesDataConverter}, Mode=OneWay}" IsReadOnly="True" />
其中 AllItems
是 SalesData
的 ObservableCollection
转换器
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();
}
}
我正在开发一个绑定到以下集合的 wpf 数据网格 class
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
很久以前我也这样做过。不幸的是,我不记得对我有帮助的 post。这个想法包括使用 DataTable
作为 DataGrid
的 ItemsSource
和将构建数据表的转换器。
Xaml
<DataGrid GridLinesVisibility="None" ItemsSource="{Binding AllItems, Converter={StaticResource SalesDataConverter}, Mode=OneWay}" IsReadOnly="True" />
其中 AllItems
是 SalesData
ObservableCollection
转换器
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();
}
}