Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  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 VB.NET
Calculate Price using ASP.NET 2.0 GridView and VB.NET


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.

Imports System.Data.SqlClient

Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!

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 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

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.

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>

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.

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

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

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

Looking for more ASP.NET Tutorials? Click Here!





 
  Developer Resources







Server Intellect Rocks