1
How can I group data in my Datagrid?
For example
It would have as I group these values and add them in a Datagrid more or less as I described above?
1
How can I group data in my Datagrid?
For example
It would have as I group these values and add them in a Datagrid more or less as I described above?
1
Example:
Obs: Pay close attention that all codes have pecularities and must be followed to have at least the same effect
XAML File - Suggested Layout
Obs: Has been declared here xmlns:local="clr-namespace:Wpfapplication1", so you can enjoy at <Window.Resources><local:TotalSum x:Key="TotalSum" /></Window.Resources>
, Totalsum is the class responsible for summing 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>
Template 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 shall be responsible for adding together the members 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 as suggested
Note that it was used Listcollectionview, Collection class configuring 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 Result
It worked out Harry!
Browser other questions tagged c# wpf
You are not signed in. Login or sign up in order to post.
You tried to Groupstyle?
– Maniero
Yes, but I couldn’t add up the values of the clusters using it.
– Jhonas
Puts what you’ve already done. You’re using the standard WPF Datagrid, right?
– Maniero