Get the Sum of a particular column values in a DataGrid

In this article, I am going to show how we can add a column value in a DataGrid. To add a column value in this program I am using a function. In the function, I am passing the value of that particular column. Let us see how to do this through the below code.
 
The default.aspx code is:
  1. <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4.   
  5.     <head runat="server">  
  6.         <title>Count a column Value</title>  
  7.     </head>  
  8.   
  9.     <body>  
  10.         <form id="form1" runat="server">  
  11.             <div>  
  12.                 <asp:DataGrid id="MyGrid" runat="server" AutoGenerateColumns="False" CellPadding="2" CellSpacing="0" BorderStyle="Solid" BorderWidth="4" Gridlines="Both" BorderColor="ActiveCaption" ItemStyle-Font-Name="Verdana" ItemStyle-Font-Size="10pt" HeaderStyle-Font-Name="Verdana" HeaderStyle-Font-Size="14pt" HeaderStyle-Font-Bold="True" HeaderStyle-BackColor="#ccffcc" FooterStyle-Font-Name="Verdana" FooterStyle-Font-Size="10pt" FooterStyle-Font-Bold="True" FooterStyle-ForeColor="White" FooterStyle-BackColor="Blue" OnItemDataBound="MyDataGrid_ItemDataBound" ShowFooter="True">  
  13.                     <Columns>  
  14.                         <asp:BoundColumn HeaderText="Product ID" DataField="ProductID" />  
  15.                         <asp:BoundColumn HeaderText="Product Name" DataField="ProductName" />  
  16.                         <asp:BoundColumn HeaderText="Unit In Price" DataField="UnitPrice" ItemStyle-HorizontalAlign="center"> </asp:BoundColumn>  
  17.                         <asp:BoundColumn HeaderText="Units In Stock" DataField="UnitsInStock" ItemStyle-HorizontalAlign="center" HeaderStyle-HorizontalAlign="Center">  
  18.                         </asp:BoundColumn>  
  19.                     </Columns>  
  20.                 </asp:DataGrid>  
  21.             </div>  
  22.         </form>  
  23.     </body>  
  24.   
  25. </html> 
The Default.aspx.cs
  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Web;  
  5. using System.Web.Security;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. using System.Web.UI.WebControls.WebParts;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Data.SqlClient;  
  11.   
  12. public partial class _Default: System.Web.UI.Page {  
  13.     private double TotalUnit = 0;  
  14.   
  15.     protected void Page_Load(object sender, EventArgs e) {  
  16.         SqlConnection con = new SqlConnection("server=Localhost; database=Northwind; uid=sa;pwd=;");  
  17.         SqlCommand cmd = new SqlCommand("SELECT ProductID, ProductName,UnitPrice, UnitsInStock  FROM Products WHERE UnitPrice > 30", con);  
  18.         try {  
  19.             con.Open();  
  20.             MyGrid.DataSource = cmd.ExecuteReader();  
  21.             MyGrid.DataBind();  
  22.             con.Close();  
  23.         } catch (Exception ex) {  
  24.             //An error occured  
  25.             HttpContext.Current.Response.Write(ex.ToString());  
  26.         }  
  27.     }  
  28.   
  29.     //This Method will calculate the column value  
  30.     private void MyTotalUnit(string Unit) {  
  31.         try {  
  32.             TotalUnit += Double.Parse(Unit);  
  33.         } catch {  
  34.             //A value was null  
  35.         }  
  36.     }  
  37.   
  38.     //Here we are bionding the data to the datagrid  
  39.     public void MyDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e) {  
  40.         if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {  
  41.   
  42.             //Here we are passing the value of a column in the MyTotalUnit function to total  
  43.             MyTotalUnit(e.Item.Cells[3].Text);  
  44.             e.Item.Cells[3].Text = string.Format("{0:0}", Convert.ToDouble(e.Item.Cells[3].Text));  
  45.         } else if (e.Item.ItemType == ListItemType.Footer) {  
  46.             e.Item.Cells[0].Text = "Total";  
  47.             e.Item.Cells[3].Text = string.Format("{0:0}", TotalUnit);  
  48.         }  
  49.     }  

When the user runs the application then the window will look like this:
 
 
Figure 1: Showing the total of Units in stock.


Similar Articles