Category / Section
How to use NumberFormatInfo in the SummaryColumn of the GridTableSummaryRow?
1 min read
In the SfDataGrid, you can use NumberFormatInfo in the SummaryColumn of the GridTableSummaryRow by deriving a new class from GridTableSummaryCellRenderer and override its OnUpdateEditBinding virtual method.
Note:
By default, summary result is displayed based on the Format specified in SummaryColumn.
C#:
public class GridTableSummaryCellRendererExt : GridTableSummaryCellRenderer { public override void OnUpdateEditBinding(DataColumnBase column, Syncfusion.UI.Xaml.Grid.GridTableSummaryCell element, object dataContext) { //Check whether the datacontext is SummaryRecordEntry var record = dataContext as SummaryRecordEntry; if (!(dataContext is SummaryRecordEntry)) return; //Process each SummaryColumn and get the display text of corresponding summary foreach (ISummaryColumn summaryColumn in record.SummaryRow.SummaryColumns) { if (!summaryColumn.MappingName.Contains(column.GridColumn.MappingName)) continue; string summarytext = string.Empty; if (record.SummaryRow.ShowSummaryInRow) summarytext = SummaryCreator.GetSummaryDisplayTextForRow(record, this.DataGrid.View); else summarytext = SummaryCreator.GetSummaryDisplayText(record, column.GridColumn.MappingName, this.DataGrid.View); if (!string.IsNullOrEmpty(summarytext)) { //Create new number format and apply it to summary columns NumberFormatInfo format = new NumberFormatInfo(); format.NumberDecimalDigits = 3; format.NumberDecimalSeparator = "*"; format.NumberGroupSeparator = ","; //Number format is applied to summary columns element.Content = Convert.ToDouble(double.Parse(summarytext, NumberStyles.Currency)).ToString("N", format); } } } }
Refer to the following code example to remove the default GridTableSummaryCellRenderer and add the customized GridTableSummaryCellRendererExt to the renderer’s collection in the SfDataGrid.
C#
public MainWindow() { InitializeComponent(); //Customized GridTableSummaryCellRendererExt is added to the CellRenderers collection grid.CellRenderers.Remove("TableSummary"); grid.CellRenderers.Add("TableSummary", new GridTableSummaryCellRendererExt()); }
The above formatting is applied to summary values as follows.
Figure 1:NumberFormatInfo is applied to summary values
Sample Links: