Adding Values in Column and Sum of the Column Values in GridView

This article shows how to determine a column sum and add some values to a column in a GridView in ASP.NET. To do that first we create a table in SQL Server and bind it to a GridView control.

The SQL Server Table is described below.

Creating Table in SQL Server Database

Now create a table named Employee with the columns Eid, EName and Salary. Set the identity property=true for Eid. Now insert data into the table. The table looks as below.

Table

Now drag and drop the GridView control from the Data controls menu. It will add the GridView control's HTML source code as given above.

  1. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White"  
  2.             BorderColor="Red" BorderStyle="Solid" BorderWidth="1px" CellPadding="2" Font-Names="Verdana"  
  3.             ShowFooter="true" Font-Size="10pt" Width="50%" DataKeyNames="Eid" GridLines="Horizontal"  
  4.             OnRowDataBound="GridView1_RowDataBound">  
  5.             <Columns>  
  6.                 <asp:BoundField DataField="Eid" HeaderText="Eid" />  
  7.                 <asp:BoundField DataField="EName" HeaderText="EName" />  
  8.                 <asp:TemplateField>  
  9.                     <ItemTemplate>  
  10.                         <asp:Label ID="lblSalary" runat="server" Text='<%# Eval("Salary")%>' />  
  11.                     </ItemTemplate>  
  12.                     <FooterTemplate>  
  13.                         <asp:Label ID="Salary" runat="server" />  
  14.                     </FooterTemplate>  
  15.                 </asp:TemplateField>  
  16.             </Columns>  
  17.             <FooterStyle BackColor="#336699" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />  
  18.             <HeaderStyle BackColor="#336699" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />  
  19. </asp:GridView>      

Now add the following namespaces.  

  1. using System.Data.SqlClient;    
  2. using System.Data; 

Now write the connection string to connect to the database.

  1. string strConnection = "Data Source=.; uid=sa; pwd=Micr0s0ft;database=master;" 

Now double-click on the page and write the following code for binding the data with the GridView.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Data.SqlClient;  
  9. public partial class Test : System.Web.UI.Page  
  10. {  
  11.     int m = 0;  
  12.     protected void Page_Load(object sender, EventArgs e)  
  13.     {  
  14.         if (!Page.IsPostBack)  
  15.         {  
  16.             BindData();  
  17.         }  
  18.     }   
  19.     private void BindData()  
  20.     {  
  21.         string constr = "data source=.; Database=master; uid=sa; pwd=Micr0s0ft;";  
  22.         string query = "SELECT Eid, EName, Salary FROM Employee";   
  23.         SqlDataAdapter da = new SqlDataAdapter(query, constr);  
  24.         DataTable table = new DataTable();  
  25.         da.Fill(table);  
  26.         GridView1.DataSource = table;  
  27.         GridView1.DataBind();  
  28.     }   
  29. }  

Now run the application and test it.

GridView Binding

Add Some Values in Column in a GridView

Now you will see how to add 10 to salary column of the GridView. To do that we write some code on the GridView1_RowDataBound event. The code looks as below:

  1. protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
  2. {  
  3.     if (e.Row.RowType == DataControlRowType.DataRow)  
  4.     {  
  5.         Label Salary = (Label)e.Row.FindControl("lblSalary");  
  6.         int addedSalary = 10 + int.Parse(Salary.Text);  
  7.         Salary.Text = addedSalary.ToString();  
  8.     }  
  9. } 

Now run the application and test it.

Add Some Values in Column

Find sum of Column values in a GridView

Now you will see how to find the sum of the salary column of the GridView. To do that we write some code on the GridView1_RowDataBound event. The code looks as below:

  1. protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
  2. {  
  3.     if (e.Row.RowType == DataControlRowType.DataRow)  
  4.     {  
  5.          Label Salary = (Label)e.Row.FindControl("lblSalary");  
  6.          //Label lblUnitsInStock = (Label)e.Row.FindControl("lblUnitsInStock");  
  7.         m = m + int.Parse(Salary.Text);  
  8.         //Table tb = new Table();  
  9.     }  
  10.     if (e.Row.RowType == DataControlRowType.Footer)  
  11.     {  
  12.         Label lblTotalPrice = (Label)e.Row.FindControl("Salary");  
  13.         lblTotalPrice.Text = m.ToString();  
  14.     }  
  15. } 
Now run the application and test it.
 
sum of Column values


Similar Articles