Search code examples
c#wpfwpfdatagrid

Using part of the Date in a ViewSource Grouping Description WPF C#


I have a ViewSource based on an observable collection of a set of objects based on a c# class called "Ticket" for a DataGrid for a WPF project in C# .net 4. The project is for a simple Helpdesk app.

The ViewSource Grouped on a Property call TktDate and all the tickets entered that day obviously grouped together in my DataGrid

However I have recently updated the code so the TktDate now not only stores the Date element but also the Time element, so clearly when I mean to Group on the Date only I am failing to do this because of the time elements (Am I correct?).. So if that was not clear here is a screen shot enter image description here

Is there a way to Group on the date part TktDate and ignore the time element for my ViewSource or must I add a new property to my class to store the date part only and group on the new property?

See the properties of the class below

 public class Ticket : INotifyPropertyChanged
{
    public int userid { get; set; }
    public int deptid { get; set; }
    public int topicid { get; set; }
    public int staffid { get; set; }
    public int priorityid { get; set; }
    public string poster { get; set; }
    public DateTime TktDate { get; set; }
    public string title { get; set; }
    public string bodyopen { get; set; }
    public string bodyclose { get; set; }
    public string timespent { get; set; }
    public string dayoffset { get; set; }
    public string sysdt { get; set; }
    public string Netdt { get; set; }
    public string Teldt { get; set; }
    public string Clidt { get; set; }
    public string status { get; set; }
    private string ticket;

    public string Tket 

In the ViewSource I group on the TktDate, which records the date the ticket is created. The grouping code is below and it worked fine

CollectionViewSource ticketViewSource = ((CollectionViewSource)(this.FindResource("ticketViewSource")));

            ticketViewSource.Source = TicketCol;
            ticketDataGrid.DataContext = ticketViewSource;
            //add grouping
            if (ticketViewSource != null)
            {
                ticketViewSource.GroupDescriptions.Clear();
                ticketViewSource.GroupDescriptions.Add(new PropertyGroupDescription("TktDate"));
                ticketViewSource.SortDescriptions.Clear();
                ticketViewSource.SortDescriptions.Add(new SortDescription("TktDate", ListSortDirection.Ascending));
            }

Solution

  • Is there a way to Group on the date part TktDate and ignore the time element for my ViewSource or must I add a new property to my class to store the date part only and group on the new property?

    You may try to group by a nested property:

    ticketViewSource.GroupDescriptions.Add(new PropertyGroupDescription("TktDate.Date"));
    

    If this doesn't work you should add a new DateTime read-only property to your Ticket class that returns TktDate.Date and group by this one.