We are setting a lot of properties of the datagrid, but most of those are to make it look pretty. You do need to notice several things, however. First note that we are setting ShowFooter to True, and settings properties for the FooterStyle. The footer is where our total line will be so we make sure we have one. We are also setting OnItemDataBound equal to "dtgOrderDetails_ItemDataBound". By doing that we are creating a place where we can compute a running total while the grid is being bound to the data source. A minor item is that we are setting HorizontalAlign to "right" for both the ItemStyle and HeaderStyle of the Quantity column (which we will be totaling). I should point out that we are using the Order Details table of the Northwind database for our example. You may run the example from the link at the bottom of the page.
|
<%@ Page Language="vb" Src="DataGridTotals.aspx.vb" Inherits="DataGridTotals" %> <html> <body> <asp:DataGrid id="dtgOrderDetails" runat="server" AutoGenerateColumns="False" ShowFooter="True" CellPadding="2" CellSpacing="0" BorderWidth="1" Gridlines="Both" HeaderStyle-Font-Name="Verdana" HeaderStyle-Font-Size="10pt" HeaderStyle-Font-Bold="True" HeaderStyle-ForeColor="White" HeaderStyle-BackColor="Coral" HeaderStyle-HorizontalAlign="Right" ItemStyle-Font-Name="Verdana" ItemStyle-Font-Size="10pt" ItemStyle-HorizontalAlign="Right" FooterStyle-Font-Name="Verdana" FooterStyle-Font-Size="10pt" FooterStyle-Font-Bold="True" FooterStyle-ForeColor="White" FooterStyle-BackColor="Coral" FooterStyle-HorizontalAlign="Right" OnItemDataBound="dtgOrderDetails_ItemDataBound"> <Columns> <asp:BoundColumn HeaderText="Order ID" DataField="orderid" /> <asp:BoundColumn HeaderText="Product ID" DataField="productid" /> <asp:BoundColumn HeaderText="Unit Price" DataField="unitprice" /> <asp:BoundColumn HeaderText="Quantity" DataField="quantity" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right" /> </Columns> </asp:DataGrid> </body> </html> |
Following the ElseIf we check to see if the row is the footer. If it is, we write "Total" in the first (0) column and our total value for quantity in the fourth (3) column of the grid. That's all there is to it! The rest of the code is the usual database connection and accessing the table in the Page_Load event.
|
Imports System Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Web.UI.HtmlControls Imports System.Data Imports System.Data.SqlClient Imports System.Configuration Public Class DataGridTotals : Inherits Page Private theTotal As double = 0 Protected dtgOrderDetails As DataGrid Private Sub CalculateTotal(theQuantity As String) theTotal += Integer.Parse(theQuantity) End Sub Public Sub dtgOrderDetails_ItemDataBound(sender As Object, e As DataGridItemEventArgs) If e.Item.ItemType = ListItemType.Item Or _ e.Item.ItemType = ListItemType.AlternatingItem Then e.Item.Cells(2).Text = string.Format("{0:c}", Convert.ToDouble(e.Item.Cells(2).Text)) CalculateTotal(e.Item.Cells(3).Text) ElseIf(e.Item.ItemType = ListItemType.Footer) e.Item.Cells(0).Text="Total" e.Item.Cells(3).Text = theTotal End If End Sub Protected Sub Page_Load(sender As object, e As EventArgs) Dim objConn As SqlConnection = New SqlConnection( _ ConfigurationSettings.AppSettings("ConnectionString")) Dim objCmd As New SqlCommand("SELECT OrderID, ProductID, " _ & "UnitPrice, Quantity " _ & "FROM [Order Details] WHERE ProductID = 5", objConn) Try objConn.Open() dtgOrderDetails.DataSource = objCmd.ExecuteReader() dtgOrderDetails.DataBind() objConn.Close() Catch exc As Exception Response.Write(exc.ToString()) End Try End Sub End Class |
You may download the code here.