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