How would you go about trying to group a certain range of years within a custom column group interval?
Wanted:
1992-2009 | 2010 | 2011 | 2012 | 2013 | 2014
Caveat: the 1992-2009 range is from the oldest date in the dataset to the oldest date with a stop at 5 years from the current year. So in 2015 the group would be 1997-2010
Current (not working) code below produces what I find unexpected:
1992-2009 | 1993 - 2009 | 1994 - 2009 etc etc
Any advice is greatly appreciated
Wanted:
1992-2009 | 2010 | 2011 | 2012 | 2013 | 2014
Caveat: the 1992-2009 range is from the oldest date in the dataset to the oldest date with a stop at 5 years from the current year. So in 2015 the group would be 1997-2010
Current (not working) code below produces what I find unexpected:
1992-2009 | 1993 - 2009 | 1994 - 2009 etc etc
Any advice is greatly appreciated
public
class
OldYearGroup : IComparable, IComparable<OldYearGroup>, IEquatable<OldYearGroup>
{
public
OldYearGroup()
{
}
internal
OldYearGroup(
int
year)
{
this
.year = year;
}
public
int
year
{
get
;
set
;
}
public
override
string
ToString()
{
if
(year > DateTime.Now.AddYears(-5).Year)
return
string
.Format(CultureInfo.InvariantCulture.NumberFormat,
"{0}"
,
this
.year);
return
string
.Format(CultureInfo.InvariantCulture.NumberFormat,
"{0}-{1}"
,
this
.year, DateTime.Now.AddYears(-5).Year);
}
public
override
int
GetHashCode()
{
return
this
.year * 7933;
}
public
override
bool
Equals(
object
obj)
{
return
this
.Equals(obj
as
OldYearGroup);
}
public
int
CompareTo(
object
obj)
{
if
(obj
is
OldYearGroup)
{
return
this
.CompareTo(obj
as
OldYearGroup);
}
throw
new
ArgumentException(
"Can not compare."
,
"obj"
);
}
public
int
CompareTo(OldYearGroup other)
{
if
(other ==
null
)
{
throw
new
ArgumentNullException(
"other"
);
}
return
this
.year.CompareTo(other.year);
}
public
bool
Equals(OldYearGroup other)
{
return
other !=
null
&&
this
.year < DateTime.Now.AddYears(-5).Year;
}
}
public
class
DefaultDateTimeGroupingDescription : PropertyGroupDescriptionBase
{
public
int
Year {
get
;
set
; }
public
DefaultDateTimeGroupingDescription()
{
}
protected
override
object
GroupNameFromItem(
object
item,
int
level)
{
var baseValue =
base
.GroupNameFromItem(item, level);
DateTime? year = ((DateTime?)baseValue);
if
(!year.HasValue)
return
null
;
return
new
OldYearGroup(year.Value.Year);
}
protected
override
void
CloneOverride(Cloneable source)
{
var castedSource = source
as
DefaultDateTimeGroupingDescription;
if
(castedSource ==
null
)
return
;
if
(castedSource.Year <
this
.Year)
this
.Year = castedSource.Year;
}
protected
override
Cloneable CreateInstanceCore()
{
return
new
DefaultDateTimeGroupingDescription();
}
}
public
class
DefaultDateTimeGroupingField : PivotGridColumnField
{
private
GroupDescription _groupDescription;
public
override
GroupDescription GroupDescription
{
get
{
if
(_groupDescription==
null
)
{
_groupDescription =
new
DefaultDateTimeGroupingDescription();
}
return
_groupDescription;
}
set
{
base
.GroupDescription = value;
}
}
}