# How to Sum Calculate Columns (expression)

1. ##### andi
18 posts
Member since:
Oct 2018

Posted 14 Nov 2018 Link to this post

I use this code to sum value from childrows to fill their parent rows :

Private Sub RadGridView1_CellValueChanged(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewCellEventArgs) Handles RadGridView1.CellValueChanged
If e.ColumnIndex = 13 AndAlso e.Column.OwnerTemplate IsNot Me.template.Templates Then
Me.EvaluateSumH2(CType(e.Row.Parent, GridViewRowInfo))
End If

If e.ColumnIndex = 7 AndAlso e.Column.OwnerTemplate IsNot Me.RadGridView1.MasterTemplate Then
Me.EvaluateSumH1(CType(e.Row.Parent, GridViewRowInfo))
End If
End Sub

Private Sub EvaluateSumH2(ByVal parent As GridViewRowInfo)
End Sub

Private Sub EvaluateSumH1(ByVal parent As GridViewRowInfo)
End Sub

But when I changed column to sum is an expression column, the sum function does not work.

Dim Total As New GridViewDecimalColumn("Total")
Total.Name = "Total"
template1.Columns("Total").Expression = "a * b* c* d* e"

Regards

2. ##### Dess | Tech Support Engineer, Sr.
3575 posts

Posted 14 Nov 2018 Link to this post

Hello, Andi,

RadGrdiView supports calculated columns which are identified by an expression. The only condition necessary to make a regular column behave like a calculated column is to set an expression to it. Additional information is available in the following help article: https://docs.telerik.com/devtools/winforms/gridview/columns/calculated-columns-(column-expressions)

This approach is suitable for expression columns which use columns from the same hierarchy level. However, if you need to display in the parent level the sum of the child rows, it is necessary to do it programmatically by iterating the ChildRows collection for each parent row and evaluating the sum. Then, the obtained result should be assigned to the respective parent cell's value.

Note that most of the forum threads are reviewed by Telerik representatives and sometimes we address the questions asked by our customers in the forums as well. However, a post in the forum doesn't guarantee you a response from the Telerik support team. Moreover, threads are handled according to license and time of posting, so if it is an urgent problem, we suggest you use a support ticket, which would be handled before a forum thread. In the support ticket you can also provide a runnable sample project demonstrating the undesired behavior that you are facing. Thus, our support staff will gladly assist you.

I hope this information helps.

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers.
3. ##### andi
18 posts
Member since:
Oct 2018

Posted 14 Nov 2018 in reply to Dess | Tech Support Engineer, Sr. Link to this post

Hello Dess,

I have iterated the ChildRows collection for each parent row and evaluating the sum programmatically.

Private Sub IterateRows()
For Each row As GridViewRowInfo In template.Rows
MsgBox("Baris Header : " & row.Cells(5).Value)
Dim hierarchyRow As GridViewHierarchyRowInfo = TryCast(row, GridViewHierarchyRowInfo)
If hierarchyRow IsNot Nothing Then
IterateChildRows(hierarchyRow)
End If
Next
End Sub

Private Sub IterateChildRows(ByVal rowInfo As GridViewHierarchyRowInfo)
Dim currentView As GridViewInfo = rowInfo.ActiveView
Dim totalsum As Double

totalsum = 0
For Each view As GridViewInfo In rowInfo.Views
rowInfo.ActiveView = view
For Each row As GridViewRowInfo In rowInfo.ChildRows
'MsgBox("Baris Detail : " & row.Cells(13).Value)
totalsum = totalsum + row.Cells(13).Value
Next
Next
'MsgBox(totalsum)
rowInfo.ActiveView = currentView
End Sub

Now, how to assigned the obtained result to the respective parent cell's value ? Thanks

4. ##### Dess | Tech Support Engineer, Sr.
3575 posts

Posted 15 Nov 2018 Link to this post

Hello, Andi,

Cells can be accessed by index or the column Name property. Please refer to the following help article demonstrating how to assign values to specific cells from the grid rows: https://docs.telerik.com/devtools/winforms/gridview/cells/accessing-cells

I hope this information helps.

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers.