我正在创建一个购物车ArrayList,它通过数据网格显示。 我已经坚持了很长时间,无法找到一种在没有SQL的情况下无法解决数据库问题的解决方案。 会话对象包含的数据超过了在数据网格中需要显示的数据,我仅将视图限制为基本要素。
我已经包含了所有用于创建和更新的方法。
// Method to create data table and insert onto page
Public Sub createShoppingCartTable()
Dim tableColumnCode As System.Data.DataColumn = New System.Data.DataColumn
With tableColumnCode
.DataType = System.Type.GetType("System.String")
.ColumnName = "ProductCode"
.DefaultValue = "0"
End With
ShoppingCartTable.Columns.Add(tableColumnCode)
Dim tableColumnName As System.Data.DataColumn = New System.Data.DataColumn
With tableColumnName
.DataType = System.Type.GetType("System.String")
.ColumnName = "Name"
.DefaultValue = "unknown"
End With
ShoppingCartTable.Columns.Add(tableColumnName)
Dim tableColumnQuantity As System.Data.DataColumn = New System.Data.DataColumn
With tableColumnQuantity
.DataType = System.Type.GetType("System.Int32")
.ColumnName = "Quantity"
.DefaultValue = "5"
End With
ShoppingCartTable.Columns.Add(tableColumnQuantity)
Dim tableColumnPrice As System.Data.DataColumn = New System.Data.DataColumn
With tableColumnPrice
.DataType = System.Type.GetType("System.Decimal")
.ColumnName = "Price"
.DefaultValue = "0"
End With
ShoppingCartTable.Columns.Add(tableColumnPrice)
Dim tableColumnTotalPrice As System.Data.DataColumn = New System.Data.DataColumn
With tableColumnTotalPrice
.DataType = System.Type.GetType("System.Decimal")
.ColumnName = "TotalPrice"
.DefaultValue = "0"
End With
ShoppingCartTable.Columns.Add(tableColumnTotalPrice)
//create rows with products added to cart
Dim localShoppingCart As ArrayList = Session("shoppingCartSession")
If Not localShoppingCart Is Nothing Then
Dim tableRow As System.Data.DataRow = ShoppingCartTable.NewRow()
Dim intMaxRows As Integer = localShoppingCart.Count - 1
Dim i As Integer
Dim localObj As New Product()
For i = 0 To intMaxRows
localObj = localShoppingCart(i)
tableRow = ShoppingCartTable.NewRow()
tableRow("ProductCode") = localObj.itemID
tableRow("Name") = localObj.itemName
tableRow("Quantity") = localObj.itemQuantity
tableRow("Price") = localObj.itemPrice
tableRow("TotalPrice") = localObj.itemQuantity * localObj.itemPrice
ShoppingCartTable.Rows.Add(tableRow)
Next
btnCheckout.Visible = True
btnContinueShopping.Visible = True
Else
btnCheckout.Visible = False
btnContinueShopping.Visible = False
End If
Me.dgDatagrid.DataSource = Me.ShoppingCartTable
Me.dgDatagrid.DataBind()
End Sub
Private Sub refreshShoppingCart(ByVal selectedIndex As Integer)
createShoppingCartTable()
With dgDatagrid
.DataSource = Me.ShoppingCartTable
.EditItemIndex = selectedIndex
.DataBind()
End With
End Sub
Private Sub dgDatagrid_EditCommand(source As Object, e As DataGridCommandEventArgs) Handles dgDatagrid.EditCommand
refreshShoppingCart(e.Item.ItemIndex)
End Sub
Private Sub dgDatagrid_CancelCommand(source As Object, e As DataGridCommandEventArgs) Handles dgDatagrid.CancelCommand
refreshShoppingCart(-1)
End Sub
Private Sub dgDatagrid_UpdateCommand(source As Object, e As DataGridCommandEventArgs) Handles dgDatagrid.UpdateCommand
Dim primaryKey = CType(e.Item.Cells(3).Controls(0), TextBox).Text
Dim number As Integer = CType(e.Item.Cells(4).Controls(0), TextBox).Text
refreshShoppingCart(-1)
End Sub