

Navigator : Home > Tutorials > Controls Tutorials > ...
Calculate Price using ASP.NET 2.0 GridView and C#
This example demonstrates how calculate total price in GridView Control.
This example demonstrates how calculate total price in GridView Control.
First, you will need to import the System.Data.SqlClient namespace.
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!
The System.Data.SqlClient namespace contains the SqlConnection and SqlCommand Classes that we need in order to operate database.In order to run this example correctly, please modify uid and pwd of the connectionstring with the uid and pwd of your database.
| using System.Data.SqlClient; |
We use the GridView1_RowDataBound1 event to do the work.
We then call the GridView1_RowDataBound1 to calculate every price.
The event occurs when a data row is bound to data in a GridView control. The code as following:
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
CalcTotal(e.Row.Cells[1].Text); } else if (e.Row.RowType == DataControlRowType.Footer) {
e.Row.Cells[0].Text = "Total"; e.Row.Cells[1].Text = string.Format("{0:c}",runningTotal); } } |
The front end CalculateTotalPriceWithGridViewCsharp.aspx page looks something like this:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" OnRowDataBound="GridView1_RowDataBound" ShowFooter="True"> <FooterStyle BackColor="Teal" ForeColor="#000066" /> <Columns> <asp:BoundField DataField="title" HeaderText="Title" /> <asp:BoundField DataField="price" HeaderText="Price" /> </Columns> <RowStyle ForeColor="#000066" /> <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /> <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> </asp:GridView> |
The flow for the code behind page is as follows.
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page {
private double runningTotal = 0; protected void Page_Load(object sender, EventArgs e) {
SqlConnection myConnection = new SqlConnection("server=Localhost;database=pubs;uid=sa;pwd=1234;"); SqlCommand myCommand = new SqlCommand("SELECT title, price FROM Titles WHERE price > 0", myConnection); try {
myConnection.Open(); this.GridView1.DataSource = myCommand.ExecuteReader(); this.GridView1.DataBind(); myConnection.Close(); } catch (Exception ex) {
HttpContext.Current.Response.Write(ex.ToString()); } } private void CalcTotal(string _price) {
try {
runningTotal += Double.Parse(_price); } catch (Exception ex) {
HttpContext.Current.Response.Write(ex.ToString()); } } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
CalcTotal(e.Row.Cells[1].Text); } else if (e.Row.RowType == DataControlRowType.Footer) {
e.Row.Cells[0].Text = "Total"; e.Row.Cells[1].Text = string.Format("{0:c}",runningTotal); } } } |
I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.
Looking for the VB.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!