如何计算动态创建的网格视图的页脚中的总计?

时间:2021-01-02 19:38:09

In my code I create some gridviews dynamically. For each of these gridviews I want to show a footerrow with the totals from each column. How can I do this?

在我的代码中,我动态创建了一些网格视图。对于每个网格视图,我想显示一个包含每列总计的英尺。我怎样才能做到这一点?

This is my code to create the gridviews:

这是我创建gridviews的代码:

            gv_arr(chart_count) = New GridView
            gv_arr(chart_count).ID = "gv" & chart_count.ToString
            gv_arr(chart_count).DataSource = dt
            If statistieksoort = LnxEnum.EBIChartType.DataGridView Then 'DataGridView
                gv_arr(chart_count).Visible = True

                'Footer voor de totalen tonen indien nodig
                gv_arr(chart_count).ShowFooter = True                    
            Else
                gv_arr(chart_count).Visible = objBichart.ShowGridView
            End If
            gv_arr(chart_count).DataBind()

1 个解决方案

#1


0  

You need to total the columns you want to total in the rowDataBound event for each data row and then insert the totals in the footer row in the same event.

您需要在每个数据行的rowDataBound事件中总计要总计的列,然后在同一事件的页脚行中插入总计。

Protected Sub gridDetails_RowDataBound(ByVal sender As Object, ByVal e As Obout.Grid.GridRowEventArgs) Handles gridDetails.RowDataBound
            If e.Row.RowType = GridRowType.DataRow Then
                subTotal+= {column with subtotal value}
                taxTotal += {column with tax value}
                discountTotal += {column with discount value}
            ElseIf e.Row.RowType = GridRowType.ColumnFooter Then
                Dim totalText1 As String = String.Empty
                Dim totalText2 As String = String.Empty
                grandTotal = subTotal + taxTotal + discountTotal
                totalText1 = "Subtotal:<br />Tax:<br />Disc / Surc:<br />Total:"
                totalText2 = String.Format("{0}<br />{1}<br />{2}<br />{3}", subTotal.ToString("c"), taxTotal.ToString("c"), discountTotal.ToString("c"), grandTotal.ToString("c"))

                e.Row.Cells(5).Text = totalText1
                e.Row.Cells(6).Text = totalText2
            End If
        End Sub

I have found this works about 95% of the time. Some times if you are dealing with strange bindings, you may have to look into the databound event for totaling. The total variables are global scoped.

我发现这个工作大约有95%的时间。有时如果你正在处理奇怪的绑定,你可能需要查看数据绑定事件以获得总计。总变量是全局范围的。

#1


0  

You need to total the columns you want to total in the rowDataBound event for each data row and then insert the totals in the footer row in the same event.

您需要在每个数据行的rowDataBound事件中总计要总计的列,然后在同一事件的页脚行中插入总计。

Protected Sub gridDetails_RowDataBound(ByVal sender As Object, ByVal e As Obout.Grid.GridRowEventArgs) Handles gridDetails.RowDataBound
            If e.Row.RowType = GridRowType.DataRow Then
                subTotal+= {column with subtotal value}
                taxTotal += {column with tax value}
                discountTotal += {column with discount value}
            ElseIf e.Row.RowType = GridRowType.ColumnFooter Then
                Dim totalText1 As String = String.Empty
                Dim totalText2 As String = String.Empty
                grandTotal = subTotal + taxTotal + discountTotal
                totalText1 = "Subtotal:<br />Tax:<br />Disc / Surc:<br />Total:"
                totalText2 = String.Format("{0}<br />{1}<br />{2}<br />{3}", subTotal.ToString("c"), taxTotal.ToString("c"), discountTotal.ToString("c"), grandTotal.ToString("c"))

                e.Row.Cells(5).Text = totalText1
                e.Row.Cells(6).Text = totalText2
            End If
        End Sub

I have found this works about 95% of the time. Some times if you are dealing with strange bindings, you may have to look into the databound event for totaling. The total variables are global scoped.

我发现这个工作大约有95%的时间。有时如果你正在处理奇怪的绑定,你可能需要查看数据绑定事件以获得总计。总变量是全局范围的。