This is a migrated thread and some comments may be shown as answers.

DateTime Column format when grouping

5 Answers 401 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Myth
Top achievements
Rank 1
Myth asked on 27 Jan 2010, 07:51 AM
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?

5 Answers, 1 is accepted

Sort by
0
Accepted
Jack
Telerik team
answered on 27 Jan 2010, 10:31 AM
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.
0
George
Top achievements
Rank 1
answered on 09 Mar 2012, 12:20 PM
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,
0
Ivan Petrov
Telerik team
answered on 13 Mar 2012, 10:46 AM
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 >>
0
Peter
Top achievements
Rank 1
answered on 23 Mar 2012, 12:03 PM
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
0
Ivan Petrov
Telerik team
answered on 27 Mar 2012, 03:44 PM
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 >>
Tags
GridView
Asked by
Myth
Top achievements
Rank 1
Answers by
Jack
Telerik team
George
Top achievements
Rank 1
Ivan Petrov
Telerik team
Peter
Top achievements
Rank 1
Share this question
or