Demo of Displaying the Sum of a Column in the DataGrid's Footer

This demo illustrates how to keep a running sum of a DataGrid column and then have that sum displayed in the DataGrid's footer.


DescriptionPage Views
How can I format numbers and date/times using ASP.NET? For example, I want to format a number as a currency.964,702
How can I convert a Recordset into an array? Also, how can I convert an array into a Recordset?238,089
I am using Access and getting a 80004005 error (or a [Microsoft][ODBC Microsoft Access Driver] The Microsoft Jet database engine cannot open the file '(unknown)' error) when trying to open a connection! How can I fix this problem?233,530
How can I quickly sort a VBScript array? 230,033
How do I display data on a web page using arrays instead of Do...While...MoveNext...???...186,006
How can I find out if a record already exists in a database? If it doesn't, I want to add it.183,511
When I get a list of all files in a directory via the FileSystemObject, they aren't ordered in any reasonable way. How can I sort the files by name? Or by size? Or by date created? Or... 168,750
For session variables to work, must the Web visitor have cookies enabled?133,635
Can I send emails without using CDONTS?130,322
How can I remove multiple spaces between words in a string? That is, if I have: Hi    there how can I get: Hi there?129,557
 Total: 2,598,135


Source Code
<%@ Import Namespace = "System.Data" %>
<%@ Import Namespace = "System.Data.SQLClient" %>
<script language="VB" runat="server">
  Dim viewCountSum as Integer = 0

  Sub Page_Load(sender as Object, e as EventArgs)
    BindData()
  End Sub
	
	
  Sub BindData()
    '1. Create a connection
    Dim myConnection as New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))

    '2. Create the command object, passing in the SQL string
    Const strSQL as String = "sp_Popularity"
    Dim myCommand as New SqlCommand(strSQL, myConnection)

    'Set the datagrid's datasource to the datareader and databind
    myConnection.Open()
    dgPopularFAQs.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    dgPopularFAQs.DataBind()
  End Sub
  
  
  Sub ComputeSum(sender As Object, e As DataGridItemEventArgs)
    'First, make sure we are dealing with an Item or AlternatingItem
    If e.Item.ItemType = ListItemType.Item OR _
          e.Item.ItemType = ListItemType.AlternatingItem then
      'Snip out the ViewCount
      Dim viewCount as Integer = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "ViewCount"))
      viewCountSum += viewCount
    ElseIf e.Item.ItemType = ListItemType.Footer then
      e.Item.Cells(1).Text = "Total: " & String.Format("{0:#,###}", viewCountSum)
    End If
  End Sub
</script>

  <asp:DataGrid runat="server" id="dgPopularFAQs"
         AutoGenerateColumns="False"
         Font-Name="Verdana" Width="85%"
         Font-Size="11pt" HorizontalAlign="Center"
         ShowFooter="True"
         OnItemDataBound="ComputeSum">
         
      <HeaderStyle BackColor="Navy" ForeColor="White" HorizontalAlign="Center"
            Font-Size="14pt" Font-Bold="True" />
            
      <FooterStyle BackColor="Navy" ForeColor="White" Font-Size="12pt" 
            Font-Bold="True" HorizontalAlign="Right" />
    
    <Columns>
      <asp:BoundColumn DataField="Description" HeaderText="Description" />
      <asp:BoundColumn DataField="ViewCount" HeaderText="Page Views" 
                DataFormatString="{0:#,###}" ItemStyle-HorizontalAlign="Right" 
                ItemStyle-Width="15%" />
    </Columns>
  </asp:DataGrid>


[Return to the article]