title: Getting total values of a certain column from GridView
body: <p>Him</p>
  
  <p>I am using a ASP.NET/VB.NET with SQL-Server-2012.</p>
  
  <p>I have a a GridView column with 3 fields and 1 template field as shown below:</p>
  
  <pre><code>&lt;asp:GridView ID="grdItems" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource3" Font-Names="Tahoma" ForeColor="#333333" GridLines="None"&gt;
      &lt;AlternatingRowStyle BackColor="White" ForeColor="#284775" /&gt;
      &lt;Columns&gt;
          &lt;asp:BoundField DataField="item_name" HeaderText="Item" SortExpression="item_name" /&gt;
          &lt;asp:BoundField DataField="item_cost" HeaderText="Cost (inc. VAT)" SortExpression="item_cost" /&gt;
          &lt;asp:BoundField DataField="item_quantity" HeaderText="Quantity" SortExpression="item_quantity" /&gt;
          &lt;asp:TemplateField HeaderText="Sub-Total (inc. VAT)"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:Label ID="TextBox3" runat="server" Text='&lt;%# Convert.ToInt32(Eval("item_quantity")) * Convert.ToDouble(Eval("item_cost"))%&gt;'&gt;&lt;/asp:Label&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField&gt;
              &lt;FooterTemplate&gt;
                  &lt;asp:Label ID="lblTotalPrice" runat="server" /&gt;
              &lt;/FooterTemplate&gt;                  
          &lt;/asp:TemplateField&gt;
      &lt;/Columns&gt;
      &lt;EditRowStyle BackColor="#999999" /&gt;
      &lt;FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /&gt;
      &lt;HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" VerticalAlign="Middle" /&gt;
      &lt;PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /&gt;
      &lt;RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Center" VerticalAlign="Middle" /&gt;
      &lt;SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /&gt;
      &lt;SortedAscendingCellStyle BackColor="#E9E7E2" /&gt;
      &lt;SortedAscendingHeaderStyle BackColor="#506C8C" /&gt;
      &lt;SortedDescendingCellStyle BackColor="#FFFDF8" /&gt;
      &lt;SortedDescendingHeaderStyle BackColor="#6F8DAE" /&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>Code behind:</p>
  
  <pre><code>Imports System.Data.SqlClient
  Imports System.Data
  
  Partial Class ProjectReport
      Inherits System.Web.UI.Page
  
      Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
  
          Dim ProjectID = Session("project_id")
          Session("ProjectID") = ProjectID
  
      End Sub
      Protected Sub grdItems_RowDataBound(sender As Object, e As GridViewRowEventArgs)
  
          Dim totalPrice As Decimal = 0
  
          If e.Row.RowType = DataControlRowType.DataRow Then
  
  
              Dim lblPrice As Label = DirectCast(e.Row.FindControl("lblTotalPrice"), Label)
  
              Dim price As Decimal = [Decimal].Parse(lblPrice.Text)
  
  
              totalPrice += price
  
          End If
  
          If e.Row.RowType = DataControlRowType.Footer Then
              Dim lblTotalPrice As Label = DirectCast(e.Row.FindControl("lblTotalPrice"), Label)
  
              lblTotalPrice.Text = totalPrice.ToString()
  
  
          End If
      End Sub
  
  
  End Class
  </code></pre>
  
  <p><strong>DataBound()</strong></p>
  
  <pre><code>   Private Sub BindData()
  
          Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
          Dim query As New SqlCommand("SELECT Items.item_name, Items.item_cost, project_items.item_quantity FROM Items INNER JOIN project_items ON items.item_id = project_items.item_id WHERE project_items.project_id = @parameter", conn)
  
          query.Parameters.AddWithValue("@UserID", Session("ProjectID"))
  
          Dim da As New SqlDataAdapter(query, conn)
  
          da.SelectCommand = query
  
          Dim table As New DataTable()
  
  
  
  
          da.Fill(table)
  
          grdItems.DataSource = table
          grdItems.DataBind()
      End Sub
  </code></pre>
  
  <p>The last column (the template field) multiplies the quantity field with the cost field.</p>
  
  <p>How do I calculate all the values (by adding) in the template field?</p>