How to customize the CaptionSummaryCell text in WPF DataGrid?
In WPF DataGrid (SfDataGrid) , GridCaptionSummaryCell displays the GroupCaption and the Caption Summary value. By default, the GroupCaption text is displayed in the {ColumnName}: {Key} – {ItemsCount} Items format. You can customize the GroupCaptionText with the help of the GroupCaptionTextFormat.
Figure 1: CaptionSummaryCell text – Default format
By using the GroupCaptionTextFormat, you cannot modify the GroupName, Group Key, and itemscount in
different formats for the available groups based on some
conditions. To achieve this, you need to derive a new class
from the GridCaptionSummaryCellRenderer and override the OnInitializeEditElement and OnUpdateEditBinding methods in the derived CustomCaptionSummarycellRenderer.
You can refer to the following
code example to derive a new class from the GridCaptionSummaryCellRenderer.
public class CustomCaptionSummaryCellRenderer : GridCaptionSummaryCellRenderer
{
….....
}
Group Key and items (Default Text) in the CaptionSummaryCell text are displayed in different formats based on the itemscount. For example, in the code example, Group Key 1000 is changed into one thousand based on the group name and the default text, items, is changed into entries in the group or elements in the group based on the itemscount.
OnInitializeEditElement method is used to initialize the content of the CaptionSummaryCell by using the grouped column details and it is invoked while creating a SpannedDataColumn in the CaptionSummaryRow.
You
can refer to the following code example to override
the OnInitializeEditElement.
/// <summary>
/// Method to initialize the CaptionSummaryCell.
/// </summary>
public override void OnInitializeEditElement(DataColumnBase dataColumn, GridCaptionSummaryCell uiElement, object dataContext)
{
if (dataContext is Group)
{
var groupRecord = dataContext as Group;
if (this.DataGrid.CaptionSummaryRow == null)
{
// get the column which is grouped
var groupedColumn = this.GetGroupedColumn(groupRecord);
//set the captionsummarycell text as customized.
uiElement.Content = GetCustomizedCaptionText(groupedColumn.HeaderText, groupRecord.Key,
groupRecord.ItemsCount);
}
else if (this.DataGrid.CaptionSummaryRow.ShowSummaryInRow)
{
uiElement.Content = SummaryCreator.GetSummaryDisplayTextForRow(groupRecord.SummaryDetails,
this.DataGrid.View);
}
else
uiElement.Content = SummaryCreator.GetSummaryDisplayText(groupRecord.SummaryDetails,
dataColumn.GridColumn.MappingName, this.DataGrid.View);
}
}
In the OnInitializeEditElement method, CaptionSummaryCell text can be customized by using grouped column name, Group Key and the itemscount. By using GetGroupedColumn method, the grouped column is obtained. ShowSummaryInRow property in GridSummaryRow is used to decide whether the summary value is displayed in the specific row or column. By using GetDisplayTextForRow method and GetDisplayText method, CaptionSummaryCell text is displayed based on the ShowSummaryInRow property.
OnUpdateEditBinding method is used to update
the CaptionSummaryCell text when there is a change in
grouping like itemscount, Group Key, and grouped column name; and it
is invoked when the CaptionSummaryRow is updated and also whenever the grid is
refreshed.
You
can refer to the following code example to
override the OnUpdateEditBinding.
/// <summary>
/// Method to update the CaptionSummaryCell.
/// </summary>
public override void OnUpdateEditBinding(DataColumnBase dataColumn, GridCaptionSummaryCell element, object dataContext)
{
if (element.DataContext is Group && this.DataGrid.View.GroupDescriptions.Count > 0)
{
var groupRecord = element.DataContext as Group;
//get the column which is grouped.
var groupedColumn = this.GetGroupedColumn(groupRecord);
if (this.DataGrid.CaptionSummaryRow == null)
{
if (this.DataGrid.View.GroupDescriptions.Count < groupRecord.Level)
return;
//set the captionsummary text as customized.
element.Content = GetCustomizedCaptionText(groupedColumn.HeaderText, groupRecord.Key,
groupRecord.ItemsCount);
}
else if (this.DataGrid.CaptionSummaryRow.ShowSummaryInRow)
{
element.Content = SummaryCreator.GetSummaryDisplayTextForRow(groupRecord.SummaryDetails,
this.DataGrid.View, groupedColumn.HeaderText);
}
else
element.Content = SummaryCreator.GetSummaryDisplayText(groupRecord.SummaryDetails,
dataColumn.GridColumn.MappingName, this.DataGrid.View);
}
}
You can refer to the following code example for GetGroupedColumn.
/// <summary>
/// Method to get the Column that is grouped.
/// </summary>
private GridColumn GetGroupedColumn(Group group)
{
var groupDesc = this.DataGrid.View.GroupDescriptions[group.Level - 1] as PropertyGroupDescription;
foreach (var column in this.DataGrid.Columns)
{
if (column.MappingName == groupDesc.PropertyName)
{
return column;
}
}
return null;
}
You can refer to the following code example for GetCustomizedCaptionText.
/// <summary>
/// Method to Customize the CaptionSummaryCell Text.
/// </summary>
private string GetCustomizedCaptionText(string columnName, object groupName, int itemsCount)
{
//entryText - instead of "Items", the entryText is assigned to Customize the CaptionSummaryCell Text.
string entryText = string.Empty;
if (itemsCount < 20)
entryText = "entries in the Group";
else if (itemsCount < 40)
entryText = "elements in the Group";
else if (itemsCount < 60)
entryText = "list in the Group";
else
entryText = "items in the Group";
if (groupName.ToString().Equals("1000"))
groupName = "One Thousand";
else if (groupName.ToString().Equals("1002"))
groupName = "Thousand and Two";
else if (groupName.ToString().Equals("1004"))
groupName = "Thousand and Four";
return string.Format("{0}: {1} - {2} {3}", columnName, groupName, itemsCount, entryText);
}
In the GetCustomizedCaptionText method, the CaptionSummaryCell text is framed and customized text is returned. You can refer to the following code example to remove the default GridCaptionSummaryCellRenderer and add the derived GridCaptionSummaryCellRenderer to the renderers’ collection in the DataGrid.
//Default CaptionSummaryCellRenderer CellRenderer is removed.
this.datagrid.CellRenderers.Remove("CaptionSummary");
//Customized CaptionSummaryCellRenderer is added.
this.datagrid.CellRenderers.Add("CaptionSummary", new CustomCaptionSummaryCellRenderer());
After the customization of the CaptionSummaryCell text, the CaptionSummaryCell is displayed as follows.
Figure 2: Customized CaptionSummaryCell text
In the above screenshot, the Group
Key is changed as One Thousand instead of 1000, thousand and two
instead of 1002 and also the default text, items, is changed as
entries in the group, elements in the group based on the itemscount.
Take a moment to peruse the WPF DataGrid - Summaries documentation, to learn more about summaries with examples.
Sample links:
Conclusion
I hope you enjoyed learning about how to customize the CaptionSummaryCell text in WPF DataGrid.