This is a migrated thread and some comments may be shown as answers.

PivotGrid Group by Week

2 Answers 114 Views
PivotGrid and PivotFieldList
This is a migrated thread and some comments may be shown as answers.
Jos
Top achievements
Rank 1
Jos asked on 02 Jan 2013, 05:23 AM
In addition to the options of group by Year, Quarter, Month or Day; I would like to be able to group by week.

2 Answers, 1 is accepted

Sort by
0
Accepted
Ivan Todorov
Telerik team
answered on 04 Jan 2013, 02:18 PM
Hi Jos,

Thank you for contacting us.

You can create custom group description to group your data by week. The following code snippet demonstrates this:
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        List<DataObject> data = new List<DataObject>();
 
        Random rand = new Random();
        for (int i = 0; i < 500; i++)
        {
            data.Add(new DataObject() {Date = DateTime.Today.AddDays(i), Value = rand.NextDouble() * 1000.0d});
        }
 
        this.radPivotGrid1.AggregateDescriptions.Add(new PropertyAggregateDescription() { PropertyName = "Value" });
        this.radPivotGrid1.RowGroupDescriptions.Add(new WeekGroupDescription() { PropertyName = "Date" });
        this.radPivotGrid1.DataSource = data;
    }
}
 
public class DataObject
{
    public DateTime Date { get; set; }
    public double Value { get; set; }
}
 
class WeekGroupDescription : PropertyGroupDescriptionBase
{
    class WeekGroupName : IComparable
    {
        int weekNumber;
 
        public WeekGroupName(int weekNumber)
        {
            this.weekNumber = weekNumber;
        }
 
        public override string ToString()
        {
            return "Week " + weekNumber;
        }
 
        public int CompareTo(object obj)
        {
            if (obj is WeekGroupName)
            {
                return weekNumber.CompareTo(((WeekGroupName)obj).weekNumber);
            }
 
            return 1;
        }
 
        public override bool Equals(object obj)
        {
            if (obj is WeekGroupName)
            {
                return weekNumber.Equals(((WeekGroupName)obj).weekNumber);
            }
 
            return false;
        }
 
        public override int GetHashCode()
        {
            return weekNumber.GetHashCode();
        }
    }
 
    protected override object GroupNameFromItem(object item, int level)
    {
        DataObject data = item as DataObject;
        if (data != null)
        {
            return new WeekGroupName(System.Globalization.DateTimeFormatInfo.CurrentInfo.Calendar.GetWeekOfYear(data.Date,
                System.Globalization.CalendarWeekRule.FirstDay,
                System.Globalization.DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek));
        }
         
        return null;
    }
 
    protected override Cloneable CreateInstanceCore()
    {
        return new WeekGroupDescription();
    }
}

I hope you find this useful. Do not hesitate to ask if you have any additional questions.

Greetings,
Ivan Todorov
the Telerik team
Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
0
Jos
Top achievements
Rank 1
answered on 15 Jan 2013, 05:41 AM
Thanks for the quick response, I have been sidetracked to another project but will try this soon.
Tags
PivotGrid and PivotFieldList
Asked by
Jos
Top achievements
Rank 1
Answers by
Ivan Todorov
Telerik team
Jos
Top achievements
Rank 1
Share this question
or