Results 1 to 2 of 2

Thread: Updating a datagrid

  1. #1

    Thread Starter
    Hyperactive Member Rocketdawg's Avatar
    Join Date
    Feb 2003
    Location
    Back in the doghouse
    Posts
    294

    Updating a datagrid

    I'm overlooking the obvious again...

    I've got a page for products. When I press 'add to cart' on that page, it updates a data table held in session and redirects to a 'my cart' page.

    Here's the Load for the 'my cart'

    VB Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.        
    3.         objCartDT = Session("Cart")
    4.         If objCartDT Is Nothing Then
    5.             lblNoItems.Text = "There are no items in your cart."
    6.         Else
    7.             lblTotal.Text = "Total: $" & GetItemTotal()
    8.             dgCart.DataSource = Session("Cart")
    9.             dgCart.DataBind()
    10.         End If
    11.     End Sub

    If they want to change a quantity, I've got this sub on the page:
    VB Code:
    1. Sub dgCart_Update(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
    2.         Dim txtQuantity As TextBox
    3.         Dim intCartID As Integer
    4.         intCartID = dgCart.DataKeys(e.Item.ItemIndex)
    5.         txtQuantity = e.Item.FindControl("txtQuantity")
    6.  
    7.         For Each objDR In objCartDT.Rows
    8.             If objDR("CartID") = intCartID Then 'this validates true
    9.                 objDR("Quantity") = Int32.Parse(txtQuantity.Text)
    10.                 Exit For
    11.             End If
    12.         Next
    13.  
    14.         lblTotal.Text = "Total: $" & GetItemTotal()
    15.         dgCart.EditItemIndex = -1
    16.         dgCart.DataSource = objCartDT
    17.         dgCart.DataBind()
    18.     End Sub

    I don't throw any errors, but I don't update the cart either . Not exactly sure where I'm screwing this up....any help?

    Thanks

  2. #2

    Thread Starter
    Hyperactive Member Rocketdawg's Avatar
    Join Date
    Feb 2003
    Location
    Back in the doghouse
    Posts
    294

    Re: Updating a datagrid

    Never mind

    Changed the load event to

    VB Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.        
    3.         objCartDT = Session("Cart")
    4.         If objCartDT Is Nothing Then
    5.             lblNoItems.Text = "There are no items in your cart."
    6.         Else
    7.             lblTotal.Text = "Total: $" & GetItemTotal()
    8.         End If
    9.  
    10.         If Not IsPostBack Then
    11.             ' need to load this data only once
    12.             BindGrid()
    13.         End If
    14.  
    15.     End Sub
    16.  
    17.     Sub BindGrid()
    18.         dgCart.DataSource = objCartDT
    19.         dgCart.DataBind()
    20.     End Sub

    And it works fine now.
    Last edited by Rocketdawg; Jul 29th, 2005 at 12:31 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width