GridView

Jun 10 2007 2:54 PM
I have the next cod in my proyect

Dim codbarras As String

Dim P As Decimal

Dim objDT As System.Data.DataTable

Dim objDR As System.Data.DataRow

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If Not IsPostBack Then

makeCart()

End If

End Sub

Function makeCart()

objDT = New System.Data.DataTable("Cart")

objDT.Columns.Add("ID", GetType(Integer))

objDT.Columns("ID").AutoIncrement = True

objDT.Columns("ID").AutoIncrementSeed = 1

objDT.Columns.Add("Quantity", GetType(Integer))

objDT.Columns.Add("Product", GetType(String))

objDT.Columns.Add("Cost", GetType(Decimal))

objDT.Columns.Add("CodBarras", GetType(String))

Session("Cart") = objDT

End Function

Protected Sub AddToCart(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click

objDT = Session("Cart")

Dim Product = ddlProducts.SelectedItem.Text

Dim blnMatch As Boolean = False

For Each objDR In objDT.Rows

If objDR("Product") = Product Then

objDR("Quantity") += txtQuantity.Text

blnMatch = True

Exit For

End If

Next

If Not blnMatch Then

objDR = objDT.NewRow

objDR("Quantity") = txtQuantity.Text

objDR("Product") = ddlProducts.SelectedItem.Text

objDR("Cost") = Decimal.Parse(ddlProducts.SelectedItem.Value)

objDR("CodBarras") = codbarras

objDT.Rows.Add(objDR)

End If

Session("Cart") = objDT

dg.DataSource = objDT

dg.DataBind()

lblTotal.Text = "$" & GetItemTotal()

End Sub

Function GetItemTotal() As Decimal

Dim intCounter As Integer

Dim decRunningTotal As Decimal

For intCounter = 0 To objDT.Rows.Count - 1

objDR = objDT.Rows(intCounter)

decRunningTotal += (objDR("Cost") * objDR("Quantity"))

Next

Return decRunningTotal

End Function

Sub Delete_Item(ByVal s As Object, ByVal e As DataGridCommandEventArgs)

objDT = Session("Cart")

objDT.Rows(e.Item.ItemIndex).Delete()

Session("Cart") = objDT

dg.DataSource = objDT

dg.DataBind()

lblTotal.Text = "$" & GetItemTotal()

I can add items to the gridview ant it works OK, but if I navegate to another
page the item disappear.

I would like to add items to the gridview from another pages.

I hope you understand my message.

Thank you.