且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将两列的值相乘并在Gridview中的第三列中显示结果?

更新时间:2022-12-12 10:28:45

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.