Question : How to calculate the sum of a bound column in a GridView control for manipulation in VB Code-Behind

I've found a few solutions that explain how to calculate and display the sum of a bound column in a GridView control, but I need to do something a little different.  I need the sum of the column, but I need to work with it elsewhere on the form.  Specifically, I need to display it in a "Sub-Total" TextBox and then (depending on the purchaser's location and the amount purchased) apply sales tax and a discount to the sub-total.

BTW...I don't need help with manipulating the sub-total.  I just need to be able to assign it to a decimal-type variable.

Thanks in advance for any help you can offer.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
----HTML----

	
		
		
	

Answer : How to calculate the sum of a bound column in a GridView control for manipulation in VB Code-Behind

Here is sample code:

Private total As Double = 0

Protected Sub gvSubscriptionShoppingCart_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        '4 here is the 0-based column index for subs-Price
        Dim price As String = e.Row.Cells(4).Text
        If Not String.IsNullOrEmpty(price) AndAlso price <> " " Then
            CalcTotal(price)
        End If
    End If
End Sub
Private Sub CalcTotal(ByVal _price As String)
    Try
        total += Double.Parse(_price, NumberStyles.Currency)
    Catch ex As Exception
        'error handling
    End Try
End Sub

Protected Sub gvSubscriptionShoppingCart_DataBound(ByVal sender As Object, ByVal e As EventArgs)
    'Set the TextBox value
    txtSubTotal.Text = total.ToString("c")
End Sub

Code adapted from this article:
http://www.aspnettutorials.com/tutorials/controls/calculate-price-aspnet-vb.aspx
Random Solutions  
 
programming4us programming4us