Ian W
Ian W

Reputation: 411

Calculating Datagrid Group totals for a column and displaying them in Group Header

There are lots of Datagrid grouping tutorials and questions but I can not seem to get my converter to calculate my items total at all.

The situation is I have a datagrid which shows items that have a time column, which is the number of hours to complete a task, the items are grouped by the date the task was completed - see below Datagrid screen shot

So the total to display is the 7.5 in the 1/8/16 group header

I believe I need to use Linq to query the ObservableCollection using the group property as a filter, but I am struggling with knowing what to do and how. The items in the collection passed to the converter seem to only by Group Items and not the items below that, it is like the items belonging to the group are not in the collection passed.

Any pointers appreciated, if I missed any code required I shall add it clearly I am not quite understanding this - thanks in advance

Here is my Converter code

 public class GroupTotalsToStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is ReadOnlyObservableCollection<Object>)
        {
            var items = (ReadOnlyObservableCollection<Object>)value;
            Decimal total = 0;
            foreach (GroupItem gi in items)
            {
                //sometype of query to add up timespent

            }
            return total.ToString();
        }
        return "";
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value;
    }

}

My Datagrid xmal is

<DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" Height="271" ItemsSource="{Binding}" 
                          Name="enteredticketsDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" Width="435" 
                           Background="Black" RowBackground="Black" 
                      AlternatingRowBackground="DarkBlue"
                          MouseDoubleClick="enteredticketsDataGrid_MouseDoubleClick" Cursor="Arrow" 
                          IsReadOnly="True" ToolTip="Select the Date From and To and Refresh the grid. Dounle click on a return ticket to open the Edit screen for that ticket.">
                    <DataGrid.Columns>
                        <DataGridTextColumn x:Name="ticketDateColumn" Binding="{Binding Path=TicketDate, StringFormat=\{0:dd/MM/yy\} }" Header="Date" Width="Auto" />
                        <DataGridTextColumn x:Name="statusColumn" Binding="{Binding Path=status}" Header="status" Width="SizeToHeader" />
                        <DataGridTextColumn x:Name="numberColumn" Binding="{Binding Path=number}" Header="number" Width="Auto" />
                        <DataGridTextColumn x:Name="timespentColumn1" Binding="{Binding Path=timespent}" Header="Time" Width="SizeToHeader" />
                        <DataGridTextColumn x:Name="subjectColumn" Binding="{Binding Path=subject}" Header="Subject" Width="Auto" />


                    </DataGrid.Columns>
                    <DataGrid.GroupStyle>
                        <!-- Style for groups at top level. -->
                        <GroupStyle>
                            <GroupStyle.ContainerStyle>
                                <Style TargetType="{x:Type GroupItem}">
                                    <Setter Property="Margin" Value="0,0,0,5"/>
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                                <Expander IsExpanded="False" Background="#FF112255" BorderBrush="#FF002255" Foreground="#FFEEEEEE" BorderThickness="1,1,1,5">
                                                    <Expander.Header>
                                                        <DockPanel>
                                                            <TextBlock FontWeight="Bold" Text="{Binding Path=Name, Converter={StaticResource StringToDateTimeConverter }}" Margin="5,0,0,0" Width="100"/>
                                                            <TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount}"/>
                                                            <!--<TextBlock FontWeight="Bold" Text="Test"/>-->
                                                            <TextBlock Text="{Binding  Path=Text, Converter={StaticResource GroupTotalsToStringConverter}}"></TextBlock>

                                                        </DockPanel>
                                                    </Expander.Header>
                                                    <Expander.Content>
                                                        <ItemsPresenter />
                                                    </Expander.Content>
                                                </Expander>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </GroupStyle.ContainerStyle>
                        </GroupStyle>
                    </DataGrid.GroupStyle>
                </DataGrid>

Upvotes: 1

Views: 1615

Answers (1)

WPFGermany
WPFGermany

Reputation: 1649

Try this:

xaml

<Expander.Header>
     <DockPanel>
          <TextBlock FontWeight="Bold" Text="{Binding Path=Name, Converter={StaticResource StringToDateTimeConverter }}" Margin="5,0,0,0" Width="100"/>
          <TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount}"/>

          <TextBlock Text="{Binding  Path=Items, Converter={StaticResource GroupTotalsToStringConverter}, UpdateSourceTrigger=PropertyChanged}" />

     </DockPanel>
</Expander.Header>

this will give your ItemsCollection to the Converter as values. The specific Datatypes belong to your ItemsSource and can be found with debugging on ConverterCall.

Upvotes: 2

Related Questions