DateTime Column format when grouping

6 posts, 1 answers
  1. Myth
    Myth avatar
    54 posts
    Member since:
    Jan 2010

    Posted 27 Jan 2010 Link to this post

    When dragging a DateTime Column with a format string to the group-by bar, the format string is ignored. This could both look bad, as well as group incorrectly.

    For example I have a DateTime column where I only want to show the date, not the time. When grouping by this column it will show the time in the group header, and use the time values for the grouping as well.

    Anyway to solve this?
  2. Answer
    Jack
    Admin
    Jack avatar
    2333 posts

    Posted 27 Jan 2010 Link to this post

    Hi Myth,

    The group header text is fully customizable. You can find an example that describes how to customize it in our Demo application. For more details, please look at this blog article.

    The build-in grouping will always use the full date (including time) to group grid rows. However, you can customize this behavior too. For this purpose, you should handle CustomGrouping event. Please consider the sample below:

     

    void radGridView1_CustomGrouping(object sender, GridViewCustomGroupingEventArgs e)
    {
        if (e.Column.UniqueName == "C5")
        {
            DateTime time1 = (DateTime)e.CellValue1;
            DateTime time2 = (DateTime)e.CellValue2;
            if (time1.Year < time2.Year)
            {
                e.SortResult = -1;
            }
            else if (time1.Year > time2.Year)
            {
                e.SortResult = 1;
            }
            else
            {
                e.SortResult = 0;
            }               
        }
    }

    Should you have any further questions, please write back.

    Best wishes,
    Jack
    the Telerik team


    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. George
    George avatar
    62 posts
    Member since:
    Sep 2008

    Posted 09 Mar 2012 Link to this post

    Hi, just upgraded to Q1-2012 release, facing same problem that if I group by a datetime field the default formatting is not inherited by the group field. Also the code displayed above is obsolete, can you please suggest a viable way to format a datetime group field in code?

    Thanks,
  4. Ivan Petrov
    Admin
    Ivan Petrov avatar
    705 posts

    Posted 13 Mar 2012 Link to this post

    Hi George,

    The code for the CustomFormatting is still relevant, this is exactly how to handle this situation. As to the group cell text, you can read the following help article - Formatting Group Header.

    I hope this will be useful for you. Should you have further questions, I would be glad to assist.

    Regards,
    Ivan Petrov
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
  5. Peter
    Peter avatar
    1 posts
    Member since:
    Jan 2011

    Posted 23 Mar 2012 Link to this post

    Hi,
    in your code example you use e.Column. This is marked as obsolete in the Q3-2011 release we are using.
    Please update you code on how to handle a specific column.
    void radGridView1_CustomGrouping(object sender, GridViewCustomGroupingEventArgs e)
    {
        if (e.Column.UniqueName == "C5")
        {

    We are using the same pattern in our code but e.Column is allways null - so it's not working.
    this.batchGridView.EnableCustomGrouping = true;
    this.batchGridView.CustomGrouping += new GridViewCustomGroupingEventHandler(batchGridView_CustomGrouping);
    this.batchGridView.GroupSummaryEvaluate += new GroupSummaryEvaluateEventHandler(batchGridView_GroupSummaryEvaluate);                    
    ...
      
    private void batchGridView_CustomGrouping(object sender, GridViewCustomGroupingEventArgs e)
    {
        if (e.Column.Name == "PROD_SLUT_DATO")
        {
            DateTime d = (DateTime)e.Row.Cells["PROD_SLUT_DATO"].Value;
            e.GroupKey = d.ToString(GridServices.GetDataTimeFormat());
        }
    }
      
    private void batchGridView_GroupSummaryEvaluate(object sender, GroupSummaryEvaluationEventArgs e)
    {
        e.FormatString = e.Group.Key.ToString();
    }

    Regards Peter
  6. Ivan Petrov
    Admin
    Ivan Petrov avatar
    705 posts

    Posted 27 Mar 2012 Link to this post

    Hello Peter,

    Thank you for writing.

    Because you can group by more than one column on more than one level, you should use the RadGridView GroupDescriptors property to get the column name. For example if have grouped on only one column you can use the following code to get the column name:
    if (e.Template.GroupDescriptors[0].GroupNames[0].PropertyName == "column name")
    {
        //Group by column
    }

    Also, I would recommend that you check the current context in the GroupSummaryEvaluate event handler. This event is fired for both groups and summary rows and you have to handle the event accordingly. The e.Context property holds the GridViewGroupRowInfo or the GridViewSummaryRowInfo that fired the event. A simple check for the type of the e.Context content should suffice.

    I hope this will help. If you need further assistance, I would be glad to provide it.

    Greetings,
    Ivan Petrov
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Back to Top