Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
3914
Format a DateTime column
posted

Hi,

How can I format a DataTime column in XamPivotGrid.

 

Thanks

Sangeetha

Parents
  • 8831
    Verified Answer
    posted

    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.

Reply Children