Adding sum column

I have loaded grid with data, groups etc. I try to make sum in grups via context menu.


So, ma code menu click:

 grid.CaptionSummaryRow.SummaryColumns.Add(New GridSummaryColumn With {
                        .Name = "Columnx",
                        .SummaryType = SummaryType.DoubleAggregate,
                        .Format = "{Sum}",
                            .MappingName = grid.Columns(columnIndex).MappingName
                        })


And recive error:  System.NullReferenceException

But if I copy grid summary row:

            Dim captionSummaryRow As New GridSummaryRow With {
            .Name = "CaptionSummary",
            .ShowSummaryInRow = False,
            .Title = "{Key}",
            .TitleColumnCount = 1
        }
            Dim i As Integer = 1
            Dim st As GridSummaryColumn
            For Each st In grid.CaptionSummaryRow.SummaryColumns
                If st.MappingName <> grid.Columns(columnIndex).MappingName Then
                    captionSummaryRow.SummaryColumns.Add(New GridSummaryColumn With {
                    .Name = "Column" & i,
                    .SummaryType = SummaryType.DoubleAggregate,
                    .Format = st.Format,
                    .MappingName = st.MappingName
                    })
                    i += 1
                End If
            Next
            i += 1
            captionSummaryRow.SummaryColumns.Add(New GridSummaryColumn With {
                    .Name = "Column" & i,
                    .SummaryType = SummaryType.DoubleAggregate,
                    .Format = "{Sum}",
                        .MappingName = grid.Columns(columnIndex).MappingName
                    })
            grid.CaptionSummaryRow = captionSummaryRow


The column have, sum.

It's normal or I make soething wrong?


3 Replies

VS Vijayarasan Sivanandham Syncfusion Team October 3, 2022 12:24 PM UTC

Hi Michal Dziubek,

The reported problem occurs due to CaptionSummaryRow not initialized in SfDataGrid. You can resolve the reported problem by initialize the GridSummaryRow and assign to CaptionSummaryRow in SfDataGrid. Please refer to the below code snippet,

Public Sub OnSummaryRowClicked(ByVal sender As Object, ByVal e As EventArgs)

              'Here initialize the CaptionSummaryRow

              sfDataGrid1.CaptionSummaryRow = New GridSummaryRow()

              sfDataGrid1.CaptionSummaryRow.ShowSummaryInRow = False

              sfDataGrid1.CaptionSummaryRow.SummaryColumns.Add(New GridSummaryColumn() With {

                             .Name = "Columnx",

                             .SummaryType = SummaryType.DoubleAggregate,

                             .Format = "{Sum}",

                             .MappingName = sfDataGrid1.Columns(0).MappingName

              })

              Me.sfDataGrid1.GroupColumnDescriptions.Add(New GroupColumnDescription() With {.ColumnName = sfDataGrid1.Columns(0).MappingName})

End Sub


UG Link: https://help.syncfusion.com/windowsforms/datagrid/summaries?cs-save-lang=1&cs-lang=vb#displaying-caption-summary-for-column

Please find the sample in the attachment and let us know if you have any concerns about this.


Regards,

Vijayarasan S

If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.





Attachment: SfDataGridDemo_a2bb7ae1.zip


MD Michal Dziubek October 3, 2022 04:32 PM UTC

Hi Vijayarasan S,


Thanks for the answer, but you didn't understand me. On form load event I load data to grid, add group and starting sum one column. 


I want to give the user the option to add sums in other columns. I display totals in columns. So it adds to an already existing collection. I don't see the need to recalculate all the sums again.


In the attachment there is an example showing my functionality.


I changed Form1 class from yours example:


Partial Public Class Form1

Inherits Form
Dim columnIndex As Integer
Public Sub New()
InitializeComponent()
sfDataGrid1.DataSource = (New ViewModel()).Orders
Me.sfDataGrid1.RecordContextMenu = New ContextMenuStrip()
Me.sfDataGrid1.RecordContextMenu.Items.Add("SummaryRow", Nothing, AddressOf OnSummaryRowClicked)
AddHandler Me.sfDataGrid1.ContextMenuOpening, AddressOf ContextMenuOpening
Me.sfDataGrid1.GroupColumnDescriptions.Add(New GroupColumnDescription() With {.ColumnName = "OrderID"})


'Starting default grid groups and sum
Dim captionSummaryRow As New GridSummaryRow With {
.Name = "CaptionSummary",
.ShowSummaryInRow = False,
.Title = "{Key}",
.TitleColumnCount = 1
}
Dim summaryColumn1 As New GridSummaryColumn With {
  .Name = "Column1",
  .SummaryType = SummaryType.DoubleAggregate,
  .Format = "{Count}",
  .MappingName = "CustomerID"
  }
captionSummaryRow.SummaryColumns.Add(summaryColumn1)
Me.sfDataGrid1.CaptionSummaryRow = captionSummaryRow
End Sub
Public Sub ContextMenuOpening(sender As Object, e As ContextMenuOpeningEventArgs)
columnIndex = e.ColumnIndex - sfDataGrid1.GroupColumnDescriptions.Count
End Sub
Public Sub OnSummaryRowClicked(ByVal sender As Object, ByVal e As EventArgs)
'Here initialize the CaptionSummaryRow
Dim captionSummaryRow As New GridSummaryRow With {
.Name = "CaptionSummary",
.ShowSummaryInRow = False,
.Title = "{Key}",
.TitleColumnCount = 1
}
Dim i As Integer = 1
Dim st As GridSummaryColumn
For Each st In sfDataGrid1.CaptionSummaryRow.SummaryColumns
If st.MappingName <> sfDataGrid1.Columns(columnIndex).MappingName Then
captionSummaryRow.SummaryColumns.Add(New GridSummaryColumn With {
.Name = "Column" & i,
.SummaryType = SummaryType.DoubleAggregate,
.Format = st.Format,
.MappingName = st.MappingName
})
i += 1
End If
Next
i += 1
captionSummaryRow.SummaryColumns.Add(New GridSummaryColumn With {
.Name = "Column" & i,
.SummaryType = SummaryType.DoubleAggregate,
.Format = "{Count}",
.MappingName = sfDataGrid1.Columns(columnIndex).MappingName
})
sfDataGrid1.CaptionSummaryRow = captionSummaryRow
End Sub
'Public class OrderInfo...
'Public Class ViewModel...


End Class

Attachment: SfDataGridDemo_6e69a3e9.zip


VS Vijayarasan Sivanandham Syncfusion Team October 4, 2022 02:49 PM UTC

Hi Michal Dziubek,

Your requirement to extend the summary column at runtime in CaptionSumamry can be achieved by retrieving the summary column and assigning modified CaptionSumamry into SfDataGrid. Please refer to the below code snippet.

'Here already we have added the summary column with 1(ex:Column1). So,starting with 2 (ex:Column2)

Dim i As Integer = 2

Public Sub OnSummaryRowClicked(ByVal sender As Object, ByVal e As EventArgs)

              'Here get the already added captionsummary row details

              Dim captionSummaryRow = sfDataGrid1.CaptionSummaryRow

              'Here adds the new summary column

              captionSummaryRow.SummaryColumns.Add(New GridSummaryColumn With {

                                           .Name = "Column" & i,

                                           .SummaryType = SummaryType.CountAggregate,

                                           .Format = "{Count}",

                                                          .MappingName = sfDataGrid1.Columns(columnIndex).MappingName

                                           })

              'Here assign the modified summary column details

              sfDataGrid1.CaptionSummaryRow = captionSummaryRow

              'Here increase the “i”value to differentiate the summary column name 

              i += 1

End Sub


Note: Add the Sum Format only suitable column in SfDataGrid. The format is a mismatched System.InvalidOperation exception occurs in SfDataGrid.

P
lease find the modified sample in the attachment and let us know if you have any concerns about this.


Regards,

Vijayarasan S


If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.



Attachment: ModifiedSample_17a1433e.zip

Loader.
Up arrow icon