Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hey, My question is How to multiply values of two columns and display the result in third column in gridview? Of which one column consist it textbox, in which the value is changed by the user at run time. And as soon as the value changes the result should be displayed in third column.
Here's my code below. In this if i changed the value manually from designer code then it display the result but not at the run time. How to dynamically change the value at run time?
Designer code:
ASP.NET
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" 
        AutoGenerateColumns="False">
        <Columns>
            <asp:BoundField DataField="Productid" HeaderText="Product Id" 
                InsertVisible="False" ReadOnly="True" SortExpression="Productid" HeaderStyle-Width="90px" ItemStyle-HorizontalAlign="Center"/>
            <asp:BoundField DataField="Pname" HeaderText="Product Name" SortExpression="Pname" HeaderStyle-Width="300px" ItemStyle-HorizontalAlign="Center"/>
            <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" HeaderStyle-Width="100px" ItemStyle-HorizontalAlign="Center"/>
            <asp:TemplateField HeaderText="Quantity" ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="80px"><ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text="1" Width="20px" AutoPostBack="true"></asp:TextBox></ItemTemplate></asp:TemplateField>
            <asp:TemplateField HeaderText="Total Price" ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="100px">
            <ItemTemplate><asp:Label ID="Label1" runat="server"></asp:Label></ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView><br />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:DBCS %>">
    </asp:SqlDataSource>


VB code:

VB
Public Partial Class WebForm5
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("usertype") = "User" Then
            SqlDataSource1.SelectCommand = "SELECT Productid, Pname, Price FROM Stock WHERE Productid IN (SELECT Productid FROM Cart WHERE Userid='" & Session("userid").ToString & "' AND Datetoday='" & Date.Today & "' AND Payment='No')"
        Else
            Response.Redirect("Account.aspx")
        End If
    End Sub

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim cellquantity As TableCell = e.Row.Cells(3)
            Dim tb As TextBox = CType(cellquantity.FindControl("TextBox1"), TextBox)
            Dim celltotalprice As TableCell = e.Row.Cells(4)
            Dim lbl As Label = CType(celltotalprice.FindControl("Label1"), Label)
            lbl.Text = Convert.ToString(Convert.ToInt32(e.Row.Cells(2).Text) * Convert.ToInt32(tb.Text))
        End If
    End Sub
End Class

Please Help.
Thankyou in Advance.
Posted

better you change your query like this for improve performance

SQL
SELECT Productid, Pname, Price,quantity,(price*quantity) as Total FROM Stock


if you calculate at your RowDatabound ,its take some time for loading.
 
Share this answer
 
Comments
Anirudha Mahadik 13-Feb-14 8:12am    
Thankyou, King_Fisher.
King Fisher 13-Feb-14 23:13pm    
welcome
Try the Multiplication Part through Javascript by adding TextBox Change event.

Refer

gridview-column-multiplication[^]

multiplication-of-two-columns-in-gridview-and-display-the-result-in-third-column-[^]

Note : The link references code consists of C#. Use a converter to convert it to VB.
 
Share this answer
 
Comments
OPees 13-Feb-14 6:53am    
+5 :)
Happy coding
JoCodes 13-Feb-14 8:08am    
Thanks :)
Anirudha Mahadik 13-Feb-14 8:12am    
Thankyou, JoCodes.
JoCodes 13-Feb-14 8:21am    
Welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900