我正在为我的培训网站开发购物车。
代码工作到现在为止,但网格视图页脚不显示列价格的总和,而只显示添加到购物车中的最后价格。
这是我的代码:
Private Sub BindGrid()
Dim constr As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
Dim con As New SqlConnection()
con.ConnectionString = constr
con.Open()
' Query SQL
Using cmd As New SqlCommand("SELECT Id,NomeProdotto,PrezzoProdotto,Quantità FROM aProdotti WHERE ID=" & Request.QueryString("ID").ToString & "", con)
Using sda As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
sda.Fill(dt)
Dim SessionDt As New DataTable()
SessionDt = Session("dt")
If Not SessionDt Is Nothing Then
If (dt.Rows.Count > 0) Then
Dim dr As DataRow
dr = SessionDt.NewRow()
dr("Id") = dt.Rows(0)("Id").ToString()
dr("NomeProdotto") = dt.Rows(0)("NomeProdotto").ToString
dr("PrezzoProdotto") = dt.Rows(0)("PrezzoProdotto").ToString
dr("Quantità") = dt.Rows(0)("Quantità").ToString
SessionDt.Rows.Add(dr)
End If
Session("dt") = SessionDt
GridView1.DataSource = SessionDt
GridView1.DataBind()
'Calculate Sum and display in Footer Row
Dim total As Decimal = dt.AsEnumerable().Sum(Function(row) row.Field(Of Decimal)("PrezzoProdotto"))
GridView1.FooterRow.Cells(1).Text = "Total"
GridView1.FooterRow.Cells(1).HorizontalAlign = HorizontalAlign.Right
GridView1.FooterRow.Cells(2).Text = total.ToString("N2")
Else
Session("dt") = dt
GridView1.DataSource = dt
GridView1.DataBind()
'Calculate Sum and display in Footer Row
Dim total As Decimal = dt.AsEnumerable().Sum(Function(row) row.Field(Of Decimal)("PrezzoProdotto"))
GridView1.FooterRow.Cells(1).Text = "Total"
GridView1.FooterRow.Cells(1).HorizontalAlign = HorizontalAlign.Right
GridView1.FooterRow.Cells(2).Text = total.ToString("N2")
End If
End Using
End Using
End Sub
页面加载:
If Not Me.IsPostBack Then
Me.BindGrid()
End If
网格视图中的最终输出不是列中所有价格的总和,而是仅添加的最后一个。怎么了?
最后,我希望在用户想要从购物车中删除项目的情况下为“删除行”添加一列。如何在没有sql数据源的情况下实现这一点? 或者我如何实现可编辑列?让我更好地解释,如果我想只有数量列可编辑而其他列被锁定。我该怎么做?
感谢所有人。