Hi all, my name is Tsvetan Raykov and I’m a developer on the WinForms RadGridView team. With this post I am starting a thread that will explain some of the great, but less known features of RadGridView for WinForms.

One of the commonly used features in RadGridView is Grouping. Many of our users have requested having the option to change its default format and appearance. This is finally possible in the latest Q2 2008 version and I am going to cover the formatting basics in this post. The example and code shown here are available for download at the bottom of this post.

 

Creating Groups

 

Let’s start with the obvious – creating groups. In order to group the grid, you need to add at least one GridGroupByExpression:

 

GridGroupByExpression expression = 
new GridGroupByExpression("City GroupBy City");
this.radGridView1.MasterGridViewTemplate.GroupByExpressions.Add(expression);

 

In our example we have added grouping by city:

grid_grouping1

 

Formatting the Group Headers

 

By default, RadGridView comes with preset group header formatting. However, it is simple to change it by using the FormatString property of the expression:

 

expression.DefaultFormatString = "Group by: {0}, Value: {1}";

 

where the {0} parameter of the format string is the name of the column that is grouped, and the {1} parameter is the value. If you want to have full control of what is displayed in the group headers, you should process the GroupSumaryEvaluate event:

 

void radGridView1_GroupSumaryEvaluate(object sender, 
GroupSummaryEvaluationEventArgs e)
{
e.FormatString = "" + e.Value + " has " + e.Group.RowCount + " row(s).";
}

 

To change the group header visual appearance, we can use the ViewRowFormatting event:

 

void radGridView1_ViewRowFormatting(object sender, RowFormattingEventArgs e)
{
if (e.RowElement is GridGroupHeaderRowElement)
{
if (e.RowElement.IsCurrent)
{
e.RowElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
e.RowElement.BackColor = Color.FromArgb(255, 255, 198);
e.RowElement.DrawFill = true;
}
else
{
e.RowElement.DrawFill = false;
}
}
}

 

The image below shows the changed grouping style:

grid_grouping2

 

Group Summaries

 

It is also possible to present more complex information in the group header by using Group Summaries. For example we can show the sum of the values of a specific column inside the group. To do this we need to add a GridViewSummaryItem that will calculate the sum:

 

this.radGridView1.MasterGridViewTemplate.SummaryRowGroupHeaders[0].
Add(new GridViewSummaryItem("Experience", "; sum {0}", GridAggregateFunction.Sum));

 

Now we can do a little trick and get the calculated value by iterating through the Aggregates collection of the DataGroup:

 

void radGridView1_GroupSumaryEvaluate(object sender, GroupSummaryEvaluationEventArgs e)
{
if (e.SummaryItem.FieldName == "Experience")
{
e.FormatString = " ";
}
else if (e.SummaryItem.FieldName == "City")
{
decimal sum = 0;
foreach (Aggregate aggregate in e.Group.Aggregates)
{
if (aggregate.AggregateFunction == GridAggregateFunction.Sum
&& aggregate.FieldName == "Experience")
{
sum = (decimal)aggregate.Value;
}
}
if (e.Group.RowCount == 1)
{
e.FormatString = string.Format(
"{0} team has one member and his work experience is {1} year(s).",
e.Value, sum);
}
else
{
e.FormatString = string.Format(
"{0} team contains {1} members with summary work experience of {2} year(s).",
e.Value, e.Group.RowCount, sum);
}
}
}

 

And here is the result:

grid_grouping3

You can download the source files from here .

I know this feature will be very useful to many of you, so I’ll be very happy to have your feedback.


Comments

Comments are disabled in preview mode.