Display Sum Total in Data Grid

原创 2004年09月29日 12:43:00

The original idea and the implementation:

The idea was to use the ItemDataBound event of the DataGrid control, grabbing the corresponding item value of all the rows of the data grid and summing it up. Then we could display the total in the footer. Please see the code snippet below:

PROTECTED As sender myDataGrid_ItemDataBound(ByVal Sub System.Object,
ByVal e As DataGridItemEventArgs) Handles myDataGrid.ItemDataBound        
Select Case e.Item.ItemType
            Case ListItemType.Item, ListItemType.AlternatingItem
                interimTotal += CType(e.Item.Cells(3).Text, Double)
            Case ListItemType.Footer
                e.Item.Cells(2).Text = "TOTAL: "
                e.Item.Cells(3).Text = interimTotal.ToString
        End Select
End Sub

The code snippet shows the event method which is invoked whenever a row is bound with the data from the data source (the binding is not shown and it is assumed that the data source is a DataReader).

Each item in the data grid is one of these types:

  • Item (Regular row)
  • AlternatingItem (Alternating row)
  • Header
  • Footer
  • Separator
  • SelectedItem
  • Pager
  • EditItem

The only items we are interested in are the Item and AlternatingItem types (and Footer of course). So we catch them with a Select Case statement. We need to remember that this method would be invoked for each row of the data grid. Items in a DataGrid control correspond to rows of output. Each Item has an array of Cells. Each column of the row in question is depicted as a Cell. The variable interimTotal is a Private variable in the code behind class. Once the Footer is reached the sum total could be displayed.

An alternative approach

The above solution works really well if the number of rows is small. If the rows are more than, say 500 rows, then we may need to adopt an alternative approach. The reason for this is that there would be a performance penalty as the method is invoked and all the work is done for each and every row of the data reader.

An alternative approach would be to use the DataSet. Using DataSet would be logically appropriate because what we are doing here is holding the data for a moment for manipulating the same. The DataReader, as we all know, is useful for grabbing the data and throwing it out for display.

The code snippet would look like as given below:

myDataGrid.DataSource = ds
myDataGrid.DataBind()
sumTotal = ds.Tables(0).Compute("SUM(amt_txn)", String.Empty).ToString
lblMessage = “TOTAL :” & sumTotal

The code snippet shows the usage of Compute() method of DataTable object. The amt_txn is the column in the DataTable for which we needed the sum total. You could place the lblMessage below the DataGrid.

Conclusion

Before you throw that stone at me, let me defend myself. I know that I could not display the TOTAL in the footer of the DataGrid.  I could, just by looping through the Items collection (of the DataGrid) and checking the ItemType, but that would be the same as the last example.

 

display:grid布局

grid布局即网格布局,每个网格都有起止线,每两条线之间都有设定好的距离 1. 网格列宽设置 首先设置网格的父级元素display:grid grid-template-colums:...
  • u012657197
  • u012657197
  • 2018年01月17日 11:23
  • 175

工业化、响应式的的CSS3 grid布局应用及其向后兼容问题-- 下 --(翻译自smashingmagazine,有删改)

接上,[工业化、响应式的的CSS3 grid布局应用及其向后兼容问题– 上 –(翻译自smashingmagazine,有删改)](工业化、响应式的的CSS3 grid布局应用及其向后兼容问题– 上 ...
  • github_36487770
  • github_36487770
  • 2017年07月20日 19:05
  • 967

MVC-Chart_WebGrid 显示漂亮chart

 原文:http://www.tuicool.com/articles/maQrYn Demo_Chart_WebGrid Two Part: (1) design a table...
  • liuxufeiyang000
  • liuxufeiyang000
  • 2015年03月25日 15:43
  • 653

CSS Grid布局指南

简介 CSS Grid布局 (又名"网格"),是一个基于二维网格布局的系统,主要目的是改变我们基于网格设计的用户接口方式。如我们所知,CSS 总是用于网页的样式设置,但它并没有起到很好的作用。刚开始...
  • ceshi986745
  • ceshi986745
  • 2016年06月22日 12:18
  • 9776

Cognos函数(六) - total的使用

这回介绍个函数,total,这个是很常用的,
  • jolingogo
  • jolingogo
  • 2014年07月29日 22:09
  • 4083

No Data To Display-fusionchart

如题,fusionchart抛出这个错误,本身是告诉我们数据错误。 出现的原因便锁定到数据错误,这就是最虐心的事情了,我一一说来: 我做了下它的封装,放到了ext的Ext.container....
  • xiaohan1990718
  • xiaohan1990718
  • 2013年11月03日 22:40
  • 1229

Extjs中GridSummary实现EditorGridPanel的合计功能

在使用gridpanel中,可以使用插件Ext.ux.grid.GroupSummary,实现分组统计,如图所示。 但通常情况下,我们也需要不进行分组统计,直接对数据进行合计显示,如下图: ...
  • xuxu198899223
  • xuxu198899223
  • 2014年01月13日 14:31
  • 1821

CxGrid 修改去除 no data to display 的两个方法

CxGrid在没有记录时会显示no data to display字样,如果想去除或修改 方法一、 在TcxGridDBTableView ==>optionsview==>nodatatodisp...
  • zisongjia
  • zisongjia
  • 2017年05月25日 17:22
  • 1098

ie10 grid 网格布局

目前HTML5的布局方式有很多:float,定位,弹性布局,分栏等。而在移动开发中,需要考虑到布局能够适应到不同的设备、方向和屏幕尺寸,可能还需要一些结构的更改:    网格布局是一种新的布局方法,...
  • yuzhitangxiangrong
  • yuzhitangxiangrong
  • 2017年05月15日 22:53
  • 354

CSS Grid布局这样玩

自从去年年半年开始,CSS Grid布局的相关教程在互联网上就铺天盖地,可谓是声势浩大。就针对于Web布局而言,个人认为Grid布局将是Web布局的神器,它改变了以往任何一种布局方式或者方法。不管以前...
  • hj7jay
  • hj7jay
  • 2017年04月25日 09:34
  • 1996
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Display Sum Total in Data Grid
举报原因:
原因补充:

(最多只允许输入30个字)