Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
Tutorial RSS
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Controls - Calculate Price using ASP.NET 2.0 GridView and C#
Calculate Price using ASP.NET 2.0 GridView and C#


ASP.NET Controls Tutorial

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.

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;

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

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:

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);
}
}

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

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>

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

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);
}
}
}

Download the Full Working Version of this Project written with Visual Studio.NET C# 2005 Here!

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!





 
  Developer Resources







Server Intellect Rocks