Gridview - calculate order value from databound fields (price) and templatefields (qty)

kenk2009
kenk2009 used Ask the Experts™
on
Gridview accurately brings in various product fields from the inventory table.  Want to be able to multiply the price times the templatefield (qty) to get an extended value for each record (see templatefield called Total Amt) and to sum all amounts into the Total Merchandise field on the aspx form.

Issue is how to reference the templatefield.  If I use databound fields for testing, the results are as expected.  But, I am not able to reference the variable qty field to get a valid result.  Either get error related to the text field or get a value of 0 in all cases.

The calculation event is drive from the command button - Recalculate Total Cost.
See image07 attached.
 
Also, here is source code for template field:
 
                    <asp:BoundField DataField="sale_val1" HeaderText="Price" ReadOnly="True" SortExpression="sale_val1" DataFormatString="{0:C2}">
                        <ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
                            Font-Underline="False" Wrap="False" Font-Names="arial" Font-Size="10pt" />
                        <HeaderStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
                            Font-Underline="False" Wrap="False" Font-Names="arial" Font-Size="10pt" />
                    </asp:BoundField>
                    <asp:TemplateField headertext="Qty">
                        <ItemTemplate>
                            <asp:TextBox style="width:30px;" id="Qty" runat="server"> 
                            </asp:TextBox>
                        </ItemTemplate>
                        <ItemStyle Font-Bold="False" Font-Italic="False" Font-Names="arial" Font-Overline="False"
                            Font-Size="10pt" Font-Strikeout="False" Font-Underline="False" />
                        <HeaderStyle Font-Bold="False" Font-Italic="False" Font-Names="arial" Font-Overline="False"
                            Font-Size="10pt" Font-Strikeout="False" Font-Underline="False" />
                    </asp:TemplateField>
                   <asp:TemplateField headertext="Total Amt">
                        <ItemTemplate>
                            <asp:TextBox style="width:30px;" id="Total" runat="server"> 
                            </asp:TextBox>
                        </ItemTemplate>
                        <ItemStyle Font-Bold="False" Font-Italic="False" Font-Names="arial" Font-Overline="False"
                            Font-Size="10pt" Font-Strikeout="False" Font-Underline="False" />
                        <HeaderStyle Font-Bold="False" Font-Italic="False" Font-Names="arial" Font-Overline="False"
                            Font-Size="10pt" Font-Strikeout="False" Font-Underline="False" />
                    </asp:TemplateField>

Open in new window

Image06.jpg
Image07.jpg
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
In that error

row.Cells(6).Text = double.Parse(row.Cells(4).Text) * double.Parse(row.Cells(5).Text)

Author

Commented:
jinal -

Thanks for being so prompt with the code.  However, when I ran the app I got a new and different error message.  See below.

"Input string was not in a correct format."

Commented:

For Each row as GridViewRow in GridView1.Rows
	TextBox total = CType(row.Cells(6).Controls(0) , TextBox)
        TextBox qty = CType(row.Cells(5).Controls(0) , TextBox)
        total.Text=  double.Parse(row.Cells(4).Text)  * double.Parse(qty.Text)
Next

Open in new window

OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Commented:
If above don't work then


For Each row as GridViewRow in GridView1.Rows
	TextBox total = CType(row.FindControl("Total"), TextBox)
        TextBox qty = CType(row.FindControl("Qty") , TextBox)
        total.Text=  double.Parse(row.Cells(4).Text)  * double.Parse(qty.Text)
Next

Open in new window

Top Expert 2013

Commented:
You have to put at check if it is a DataRow only and not a header, etc..
For Each row as GridViewRow in GridView1.Rows
If row.RowType = DataControlRowType.DataRow Then
           'all calculation code
End If
Next

Author

Commented:
This is the new code but additional errors came thru as shown on the image file which I have attached for your reference.

       For Each row As GridViewRow In GridView1.Rows
            If row.RowType = DataControlRowType.DataRow Then
                TextBox(total = CType(row.Cells(6).Controls(0), TextBox))
                TextBox(qty = CType(row.Cells(5).Controls(0), TextBox))
                total.Text = Double.Parse(row.Cells(4).Text) * Double.Parse(qty.Text)
            End If
        Next

Image10.jpg
Top Expert 2013

Commented:
You might want to have one more look at the code provided in jinal's post# 24881640
For Each row As GridViewRow In GridView1.Rows
            If row.RowType = DataControlRowType.DataRow Then
              TextBox total = CType(row.FindControl("Total"), TextBox)
              TextBox qty = CType(row.FindControl("Qty") , TextBox)
               total.Text=  double.Parse(row.Cells(4).Text)  * double.Parse(qty.Text)
            End If
        Next

Author

Commented:
guru_sami -

I followed your suggestion and recopied the code into the app.  The app immediately puts parenthesis around parts of the code as shown in the current image attachment.  Apparently, it is a step that the app takes automatically.  I must admit I am not familiar with that syntax in the normal course of writing VB code but, then again, I am not the expert with this type of app either.

I will look into it further on Monday morning.  But, any other suggestions would be welcome.

Thank you for your input.
Image03.jpg
Top Expert 2013

Commented:
How about trying this:
For Each row As GridViewRow In GridView1.Rows
  If row.RowType = DataControlRowType.DataRow Then
    Dim total As TextBox = DirectCast(row.FindControl("Total"), TextBox)        
    Dim qty As TextBox = DirectCast(row.FindControl("Qty"), TextBox)
    total.Text=  double.Parse(total.Text)  *  double.Parse(qty.Text)
End If
Next

Author

Commented:
Better and I will do some work on Monday.  I need to link the quantity and price to get the total value.  Originally, cell4 was price and cell5 was the quantity entered by the operator.  the result was the total_value field which is summed up to the total merchandise text field.

The code comment is that the info is in the wrong format (see the green highlighted info in the image).

Thank you for staying after it today.

Ken
Image04.jpg
Top Expert 2013
Commented:
sorry my bad...i made some mistake while copying the code
you are right it should be price * quantity...thats what was provided earlier...

For Each row As GridViewRow In GridView1.Rows
  If row.RowType = DataControlRowType.DataRow Then
    Dim total As TextBox = DirectCast(row.FindControl("Total"), TextBox)        
    Dim qty As TextBox = DirectCast(row.FindControl("Qty"), TextBox)
    total.Text=  double.Parse(row.Cells(4).Text)  *  double.Parse(qty.Text)
End If
Next

Commented:
In last comment i forgot to make it for vb.net


For Each row as GridViewRow in GridView1.Rows
        Dim ctotal as Control = row.FindControl("Total")
        Dim cqty as Control = row.FindControl("Qty") 
        If Not ctotal Is Nothing And cqty Is Nothing Then
        Dim total as TextBox = CType(ctotal, TextBox)
        Dim qty as TextBox = CType(cqty , TextBox)         
        total.Text=  double.Parse(row.Cells(4).Text)  * double.Parse(qty.Text)
        End if
Next

Open in new window

Please remove the " DataFormatString " attribute in the following code
<asp:BoundField DataField="sale_val1" HeaderText="Price" ReadOnly="True" SortExpression="sale_val1" DataFormatString="{0:C2}">

you can format the price field later after calculation or else before doing calculation remove the $ Symbol in price field

Let me know the status to help u further.




 
Commented:
Thanks to everyone who contributed to the solution.  Each of you added a piece that ultimately got the question resolved.

The deletion of the formating string proved to be the final element.

I attached a copy of the final code and the app page with the running totals in place.

Now, how do I accept the solution so each of you get the appropriate credit for the solution?

Again, thanks to each of you for the help.

Ken
Image01.jpg
Image02.jpg

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial