Hi,
How can I format a DataTime column in XamPivotGrid.
Thanks
Sangeetha
Hello,
Currently the default datetime hierarchy descriptor is defined as follows:
HierarchyDescriptor dateTimeDescriptor = new HierarchyDescriptor { AppliesToPropertiesOfType = typeof(DateTime) };
dateTimeDescriptor.AddLevel<DateTime>(dt => SR.GetString("AllPeriods"), "All Periods");
dateTimeDescriptor.AddLevel<DateTime>(dt => dt.Year, "Years");
dateTimeDescriptor.AddLevel<DateTime>(dt => dt.SemesterShort(), "Semesters");
dateTimeDescriptor.AddLevel<DateTime>(dt => dt.QuarterShort(), "Quarters");
dateTimeDescriptor.AddLevel<DateTime>(dt => dt.MonthShort(), "Months");
// set the sort order of months
Expression<Func<DateTime, int>> expression = pd => pd.Month;
dateTimeDescriptor.LevelDescriptors[4].OrderByKeyExpression = expression;
dateTimeDescriptor.AddLevel<DateTime>(dt => dt.Date.ToShortDateString(), "Dates");
As you can see the Dates level here operates as it returns string values. You can modify Dates level descriptor to returns a value with type of DateTime. Just copy the code above and modify only Dates level descriptor:
dateTimeDescriptor.AddLevel<DateTime>(dt => dt.Date, "Dates");
flatDataSource.HierarchyDescriptors.Add(dateTimeDataDescriptor);
When the data is visualized the specified format string will be applied to this value. You can set the format string using an attribute applied to the property
[DisplayFormat(DataFormatString = "ddd dd MM yyyy")]
public DateTime Date
{
get; set;
}
Or adding a dimension metadata:
CubeMetadata cubeMetadata = new CubeMetadata { DataTypeFullName = "[Your data type full name]", DisplayName = "My Cube Display Name" };
flatDataSource.CubesSettings.Add(cubeMetadata);
DimensionMetadata dateMetadata = new DimensionMetadata
SourcePropertyName = "Date",
DisplayName = "Dates",
AutoGenerateField = true,
DisplayFormat = "ddd dd MM yyyy"
};
cubeMetadata.DimensionSettings.Add(dateMetadata);
Best regards.
Plamen.
Great. Thank You. I have one problem. I the following line, What is the SR object? I googled and found that it belongs to Web classes. My App is not a web app, so is there an alternative to SR object?