Assuming "Date" is of type DateTime, I know it's possible to do the following:
I've replaced my DateTime property with a new custom class called MonthAndYear.
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?
<
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?