Articles in this section
Category / Section

How to programmatically add column to caption summary row in WPF DataGrid (SfDataGrid)?

2 mins read

WPF DataGrid (SfDataGrid) allows you to add column to CaptionSummaryRow at run time by adding GridSummaryColumn to CaptionSummaryRow.SummaryColumns.

dataGrid.Loaded += DataGrid_Loaded;
addColumn.Click += addColumn_Click;private void DataGrid_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
    this.dataGrid.CaptionSummaryRow = new GridSummaryRow()
    {
        ShowSummaryInRow = true,
        Title = "Total Sales: {SalesAmount} for NumberOfYears: {NumberOfYears}",
        SummaryColumns = new ObservableCollection<ISummaryColumn>()
        {
            new GridSummaryColumn()
            {
                Name = "SalesAmount",
                MappingName="Total",
                SummaryType= SummaryType.Int32Aggregate,
                Format="{Sum:c}"
            }  ,
                new GridSummaryColumn()
            {
                Name = "NumberOfYears",
                MappingName="Year",
                SummaryType= SummaryType.CountAggregate,
                Format="{Count:d}"
            }
        }
    };
}
private void addColumn_Click(object sender, RoutedEventArgs e)
{
    this.dataGrid.CaptionSummaryRow.SummaryColumns.Add(new GridSummaryColumn() { Name = "Q1Sales", MappingName = "QS1", SummaryType = SummaryType.Int32Aggregate, Format = "{Sum:d}" });
    this.dataGrid.CaptionSummaryRow.Title = "Total Sales: {SalesAmount} for NumberOfYears: {NumberOfYears} and Quaterly Sales is {Q1Sales}";
    this.dataGrid.View.CaptionSummaryRow = this.dataGrid.CaptionSummaryRow;
    this.dataGrid.RowGenerator.Items.ForEach(o =>
    {
        if (o.RowType == RowType.CaptionCoveredRow || o.RowType == RowType.CaptionRow)
            o.GetType().GetProperty("RowIndex").SetValue(o, -1);
    });
    this.dataGrid.GetVisualContainer().InvalidateMeasureInfo();
}

Demo for show How to programmatically add column to caption summary row

View WPF DataGrid CaptionSummary Demo in GitHub.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied