Search code examples
c#wpfxceed-datagrid

Is it possible to group by date, but sort by date + time in an xceed datagrid?


I'm using the (community edition) of the Extended WPF Toolkit to display records that have a DateTime column.

<xcdg:DataGridControl
        ItemsSource="{Binding ResponseInstructions, UpdateSourceTrigger=PropertyChanged}"
        ReadOnly="True"
        AutoCreateColumns="False"
    >
    <xcdg:DataGridControl.Columns>
        <xcdg:Column FieldName="Created" Title="Request made">
            <xcdg:Column.CellContentTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding StringFormat=\{0:dd/MM/yy hh:mm\}}" />
                </DataTemplate>
            </xcdg:Column.CellContentTemplate>
        </xcdg:Column>
        <!-- SNIP: Some other columns here... -->
        <xcdg:Column FieldName="Notes" Title="Notes" Width="*" />
    </xcdg:DataGridControl.Columns>
</xcdg:DataGridControl>

I'd like to be able to change the grouping behaviour of the DateTime column so that it groups by date (i.e. ignoring time), but to also retain the default sorting ability (i.e. date, then time).

If it weren't for the need to retain the default sorting functionality, I'd just change the column from DatetTime to Date. Is there a way to have my cake and eat it to?


Solution

  • eventually I figured out that the desired can be done in pure xaml also.

    here is the sample xaml

        <xcdg:Column FieldName="Created"
                        Title="Request made">
            <xcdg:Column.GroupDescription>
                <xcdg:DataGridGroupDescription PropertyName="Created.Day"/>
            </xcdg:Column.GroupDescription>
            <xcdg:Column.CellContentTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding StringFormat=\{0:dd/MM/yy hh:mm\}}" />
                </DataTemplate>
            </xcdg:Column.CellContentTemplate>
        </xcdg:Column>
    

    result

    result

    in example above the sorting is based on the full date and time where as the grouping is on the Day property of the Created(DateTime) property

    above example is based on some assumptions, I was not able to see your approach as GroupByDate resource from GroupDescription="{StaticResource GroupByDate}" was not mentioned in the question.

    EDIT

    as requested here is a way to change most aspects of the group including the top panel and the group header

    <xcdg:DataGridControl ItemsSource="{Binding ResponseInstructions}"
                          ReadOnly="True"
                          AutoCreateColumns="False">
        <xcdg:DataGridControl.Resources>
            <Style TargetType="{x:Type xcdg:GroupByItem}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Title}"
                                 Value="Created.Date">
                        <Setter Property="Content"
                                Value="Request made" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
            <DataTemplate DataType="{x:Type xcdg:Group}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Value,StringFormat=dd/MM/yy}" />
                    <TextBlock Text=": " />
                    <TextBlock Text="{Binding Items.Count}" />
                    <TextBlock Text=" request(s)" />
                </StackPanel>
            </DataTemplate>
        </xcdg:DataGridControl.Resources>
        <xcdg:DataGridControl.Columns>
            <xcdg:Column FieldName="Created"
                         Title="Request made">
                <xcdg:Column.GroupDescription>
                    <xcdg:DataGridGroupDescription PropertyName="Created.Date" />
                </xcdg:Column.GroupDescription>
                <xcdg:Column.CellContentTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding StringFormat=\{0:dd/MM/yy hh:mm\}}" />
                    </DataTemplate>
                </xcdg:Column.CellContentTemplate>
            </xcdg:Column>
            <!-- SNIP: Some other columns here... -->
            <xcdg:Column FieldName="Notes"
                         Title="Notes"
                         Width="*" />
        </xcdg:DataGridControl.Columns>
    </xcdg:DataGridControl>
    

    result

    result

    I have introduced one style for the top panel and a data template for the group header. I have used data triggers in the style to set the custom title as it seems like the data-grid has its own mechanism to retrieve the group title. Also I have modified the group property from Day to Date, as day was not the correct as it is only the day not the date.