DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 Calculate Price using ASP.NET 2.0 GridView and VB.NET

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.

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 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.
Imports 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:

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

Protected Sub GridView1_RowDataBound1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
CalcTotal(e.Row.Cells(1).Text)
e.Row.Cells(1).Text = String.Format("{0:c}", Convert.ToDouble(e.Row.Cells(1).Text))
ElseIf e.Row.RowType = DataControlRowType.Footer Then
e.Row.Cells(0).Text = "Total"
e.Row.Cells(1).Text = String.Format("{0:c}", runningTotal)
End If
End Sub

The front end CalculateTotalPriceWithGridViewVB.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.

Imports System.Configuration Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Imports System.Data.SqlClient

Partial Class _Default
Inherits System.Web.UI.Page
Private runningTotal As Double = 0

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim myConnection As New SqlConnection("server=Localhost;database=pubs;uid=sa;pwd=1234;")
Dim myCommand As New SqlCommand("SELECT title, price FROM Titles WHERE price > 0", myConnection)
Try
myConnection.Open()
Me.GridView1.DataSource = myCommand.ExecuteReader()
Me.GridView1.DataBind()
myConnection.Close()
Catch ex As Exception
HttpContext.Current.Response.Write(ex.ToString())
End Try
End Sub

Private Sub CalcTotal(ByVal _price As String)
Try
runningTotal += [Double].Parse(_price)
Catch ex As Exception
HttpContext.Current.Response.Write(ex.ToString())
End Try
End Sub

Protected Sub GridView1_RowDataBound1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
CalcTotal(e.Row.Cells(1).Text)
e.Row.Cells(1).Text = String.Format("{0:c}", Convert.ToDouble(e.Row.Cells(1).Text))
ElseIf e.Row.RowType = DataControlRowType.Footer Then
e.Row.Cells(0).Text = "Total"
e.Row.Cells(1).Text = String.Format("{0:c}", runningTotal)
End If
End Sub
End Class


Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!