This is a migrated thread and some comments may be shown as answers.

How to Sum Calculate Columns (expression)

3 Answers 181 Views
GridView
This is a migrated thread and some comments may be shown as answers.
andi
Top achievements
Rank 1
andi asked on 14 Nov 2018, 08:57 AM

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)
        parent.Cells(7).Value = Me.RadGridView1.Evaluate("Sum(Total)", parent.ChildRows)
    End Sub

    Private Sub EvaluateSumH1(ByVal parent As GridViewRowInfo)
        parent.Cells(4).Value = Me.RadGridView1.Evaluate("Sum(Total)", parent.ChildRows)
    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"
        Total.HeaderText = "Total"
        template1.Columns.Add(Total)        
        template1.Columns("Total").Expression = "a * b* c* d* e"

Please help, thanks before.

 

Regards

3 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 14 Nov 2018, 09:22 AM
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. Learn More.
0
andi
Top achievements
Rank 1
answered on 15 Nov 2018, 02:00 AM

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

 

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 15 Nov 2018, 12:13 PM
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. Learn More.
Tags
GridView
Asked by
andi
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
andi
Top achievements
Rank 1
Share this question
or