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

Grouping by Custom Object

1 Answer 17 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dylan
Top achievements
Rank 1
Dylan asked on 15 Nov 2013, 09:26 PM
Assuming "Date" is of type DateTime, I know it's possible to do the following:

<telerik:GridGroupByExpression>
     <SelectFields>
         <telerik:GridGroupByField FieldName="Date" />
     </SelectFields>
     <GroupByFields>
         <telerik:GridGroupByField FieldName="Date" SortOrder="Ascending" />
     </GroupByFields>
 </telerik:GridGroupByExpression>

I've replaced my DateTime property with a new custom class called MonthAndYear.

[Serializable]
protected class MonthAndYear : IComparable<MonthAndYear>
{
    public int Month { get; set; }
    public int Year { get; set; }
 
    public string monthString
    {
        get
        {
            return CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(Month);
        }
    }
 
    public MonthAndYear(int month, int year)
    {
        Month = month;
        Year = year;
    }
 
    public MonthAndYear(DateTime dateTime)
    {
        Month = dateTime.Month;
        Year = dateTime.Year;
    }
 
    public static MonthAndYear Now
    {
        get
        {
            return new MonthAndYear(DateTime.Now.Month, DateTime.Now.Year);
        }
    }
 
    public static MonthAndYear FromDate(DateTime date)
    {
        return new MonthAndYear(date.Month, date.Year);
    }
 
    public MonthAndYear AddMonths(int num)
    {
        DateTime tempDate = new DateTime(Year, Month, 1);
        tempDate = tempDate.AddMonths(num);
        return new MonthAndYear(tempDate.Month, tempDate.Year);
    }
 
    public MonthAndYear AddYears(int num)
    {
        int newYear = Year + num;
        return new MonthAndYear(Month, newYear);
    }
 
    public static bool operator >(MonthAndYear a, MonthAndYear b)
    {
        if (a.Year == b.Year)
            return a.Month > b.Month;
        else if (a.Year > b.Year)
            return true;
        else
            return false;
    }
 
    public static bool operator <(MonthAndYear a, MonthAndYear b)
    {
        if (a.Year == b.Year)
            return a.Month < b.Month;
        else if (a.Year < b.Year)
            return true;
        else
            return false;
    }
 
    public static bool operator >=(MonthAndYear a, MonthAndYear b)
    {
        return a > b || a == b;
    }
 
    public static bool operator <=(MonthAndYear a, MonthAndYear b)
    {
        return a < b || a == b;
    }
 
    public static bool operator ==(MonthAndYear a, MonthAndYear b)
    {
        return a.Month == b.Month && a.Year == b.Year;
    }
 
    public static bool operator !=(MonthAndYear a, MonthAndYear b)
    {
        return !(a.Month == b.Month && a.Year == b.Year);
    }
 
    public override bool Equals(object obj)
    {
        if (obj == null)
        {
            return false;
        }
        MonthAndYear m = obj as MonthAndYear;
        if ((System.Object)m == null)
        {
            return false;
        }
 
        return this.Month == m.Month && this.Year == m.Year;;
    }
 
    public override string ToString()
    {
        return new DateTime(Year, Month, 1).ToString();
    }
 
    public override int GetHashCode()
    {
        int hash = 23;
        hash = hash * 31 + Month.GetHashCode();
        hash = hash * 31 + Year.GetHashCode();
        return hash;
    }
 
    public int CompareTo(MonthAndYear other)
    {
        if (this < other) return -1;
        if (this == other) return 0;
        return 1;
    }
}

Now when I try to group by Date, I get an IndexOutOfRange exception that says "Cannot find column Date." If I remove the GridGroupByExpression, everything works just fine minus the fact that my data isn't grouped by Date. Is there a way to use custom objects in RadGrid's grouping functionality? Or at least a way to mimic grouping by DateTime?   

1 Answer, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 20 Nov 2013, 02:05 PM
Hi Dylan,

After some testing on my end I was able to replicate the described issue.

I will log this in our system, so our developers team could investigate it further in order to determine if this is a bug or a limitation with RadGrid grouping.

Regarding your question for grouping by DateTime object, this is a supported scenario and you should not experience any issues with it.

Please excuse us for any inconvenience caused by this.


Regards,
Konstantin Dikov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
Dylan
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Share this question
or