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.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="Red" BorderStyle="Solid" BorderWidth="1px" CellPadding="2" Font-Names="Verdana"
ShowFooter="true" Font-Size="10pt" Width="50%" DataKeyNames="Eid" GridLines="Horizontal"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="Eid" HeaderText="Eid" />
<asp:BoundField DataField="EName" HeaderText="EName" />
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblSalary" runat="server" Text='<%# Eval("Salary")%>' />
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="Salary" runat="server" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#336699" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
<HeaderStyle BackColor="#336699" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
</asp:GridView>
Now add the following namespaces.
using System.Data.SqlClient;
using System.Data;
Now write the connection string to connect to the database.
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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Test : System.Web.UI.Page
{
int m = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
private void BindData()
{
string constr = "data source=.; Database=master; uid=sa; pwd=Micr0s0ft;";
string query = "SELECT Eid, EName, Salary FROM Employee";
SqlDataAdapter da = new SqlDataAdapter(query, constr);
DataTable table = new DataTable();
da.Fill(table);
GridView1.DataSource = table;
GridView1.DataBind();
}
}
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:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label Salary = (Label)e.Row.FindControl("lblSalary");
int addedSalary = 10 + int.Parse(Salary.Text);
Salary.Text = addedSalary.ToString();
}
}
Now run the application and test it.
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:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label Salary = (Label)e.Row.FindControl("lblSalary");
//Label lblUnitsInStock = (Label)e.Row.FindControl("lblUnitsInStock");
m = m + int.Parse(Salary.Text);
//Table tb = new Table();
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lblTotalPrice = (Label)e.Row.FindControl("Salary");
lblTotalPrice.Text = m.ToString();
}
}
Now run the application and test it.