如何将两列的值相乘并在Gridview中的第三列中显示结果? [英] How Do I Multiply Values Of Two Columns And Display The Result In Third Column In Gridview?

查看:95
本文介绍了如何将两列的值相乘并在Gridview中的第三列中显示结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我的问题是如何将两列的值相乘并在gridview的第三列中显示结果?其中一列由文本框组成,其中值在运行时由用户更改。一旦值改变,结果应显示在第三列。

这是我的代码如下。在此,如果我从设计器代码手动更改值,则它显示结果,但不显示运行时。如何在运行时动态更改值?

设计器代码:

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: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代码:





VB code:

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.

推荐答案

ConnectionStrings:DBCS%> >
< / asp:SqlDataSource >
ConnectionStrings:DBCS %>"> </asp:SqlDataSource>





VB代码:





VB code:

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.


您可以更好地更改此类查询以提高性能



better you change your query like this for improve performance

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





如果你在RowDatabound上计算,它需要一些时间来加载。



if you calculate at your RowDatabound ,its take some time for loading.


通过添加TextBox Change事件来尝试通过Javascript实现乘法部分。



参考



gridview的柱 - 乘法 [ ^ ]



乘法的两柱合的GridView和 - 显示最结果合第三柱 - [ ^ ]



注意:链接引用代码由C#组成。使用转换器将其转换为VB。
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.


这篇关于如何将两列的值相乘并在Gridview中的第三列中显示结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆