Display Sum Total in Data Grid

xiaoxiao2021-03-06  64

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_itemdatabase (byval Sub System.Object,

Byval e as datagriditemeventargs) Handles myDataGrid.ItemDatabase

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 DataRead).

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

Item (Regular Row) ALTERNATIITEM (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 displaphyed.

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 PPPPPOOK LIKE AS GIVEN BELOW:

MyDataGrid.DataSource = DS

MyDataGrid.Database

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.

转载请注明原文地址:https://www.9cbs.com/read-111886.html

New Post(0)