How can I group data in my DataGrid?
For example
Would you like to group these values and sum them in a DataGrid more or less as I described above?
How can I group data in my DataGrid?
For example
Would you like to group these values and sum them in a DataGrid more or less as I described above?
Example:
Obs:
Pay close attention to the fact that all codes have specific requirements and must be followed to have at least the same effectXAML File - Suggested Layout
Note: I declare here the xmlns: local="clr-namespace: WpfApplication1", so you can enjoy in <Window.Resources><local:TotalSum x:Key="TotalSum" /></Window.Resources>
, where TotalSum is the class responsible for adding the items of each group. This item will be used in: <TextBlock Grid.Row="0" Grid.Column="1" HorizontalAlignment="Left" Text="{Binding Path=Items, Converter={StaticResource TotalSum}}" />
.
<Window x:Class="WpfApplication1.MainWindow" xmlns:local="clr-namespace:WpfApplication1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Activated="Window_Activated" Initialized="Window_Initialized">
<Window.Resources>
<local:TotalSum x:Key="TotalSum" />
</Window.Resources>
<Grid>
<DataGrid x:Name="GridVendas" CanUserAddRows="False" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="299" Width="497">
<DataGrid.GroupStyle>
<GroupStyle>
<!--Header Cabeçalho -->
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<!--Container Linhas -->
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<DockPanel>
<Grid DockPanel.Dock="Bottom">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<TextBlock Grid.Column="0" Text="{Binding Path=Name}" />
<TextBlock Grid.Column="0" Text=" R$: " />
<TextBlock Grid.Row="0" Grid.Column="1" HorizontalAlignment="Left" Text="{Binding Path=Items, Converter={StaticResource TotalSum}}" />
</StackPanel>
</Grid>
<ItemsPresenter />
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
</DataGrid>
</Grid>
</Window>
Model Classes - Will be responsible for WPF DataGrid Columns
public class ItemVendas
{
public String Nome { get; set; }
public Decimal Valor { get; set; }
public String Grupo { get;set;}
}
TotalSum Class - Implementing Class of IValueConverter and will be responsible for adding the elements of each group
public class TotalSum: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
decimal sum = 0;
foreach (object item in (System.Collections.ObjectModel.ReadOnlyObservableCollection<object>)value)
{
ItemVendas it = (ItemVendas)item;
sum += it.Valor;
}
return sum;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Loading - Window_Initialized Method - Examples are given at suggestion
Notice that you used ListCollectionView , the collection class you set up the grouping by property Grupo
.
private void Window_Initialized(object sender, EventArgs e)
{
ListItemVendas = new ObservableCollection<ItemVendas>();
ListItemVendas.Add(new ItemVendas() { Grupo = "Vendas à dinheiro", Nome = "Suco de Uva", Valor = 2.50M });
ListItemVendas.Add(new ItemVendas() { Grupo = "Vendas à dinheiro", Nome = "Pão de Queijo", Valor = 2.00M });
ListItemVendas.Add(new ItemVendas() { Grupo = "Vendas à Cartão", Nome = "Suco de Laranja", Valor = 3.50M });
ListItemVendas.Add(new ItemVendas() { Grupo = "Vendas à Cartão", Nome = "Quible", Valor = 4.00M });
ListItemVendas.Add(new ItemVendas() { Grupo = "Vendas à Cartão", Nome = "Suco Copo", Valor = 1.50M });
var listaView = new ListCollectionView(ListItemVendas);
listaView.GroupDescriptions.Add(new PropertyGroupDescription("Grupo"));
GridVendas.AutoGenerateColumns = true;
GridVendas.ItemsSource = listaView;
}
Final Score