PivotGrid Group by Week

3 posts, 1 answers
  1. Jos
    Jos avatar
    2 posts
    Member since:
    Oct 2012

    Posted 01 Jan 2013 Link to this post

    In addition to the options of group by Year, Quarter, Month or Day; I would like to be able to group by week.
  2. Answer
    Ivan Todorov
    Admin
    Ivan Todorov avatar
    688 posts

    Posted 04 Jan 2013 Link to this post

    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.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Jos
    Jos avatar
    2 posts
    Member since:
    Oct 2012

    Posted 14 Jan 2013 Link to this post

    Thanks for the quick response, I have been sidetracked to another project but will try this soon.
Back to Top