开发者

How to delete an item from a collection that is data bound to a GridView

开发者 https://www.devze.com 2023-03-22 07:35 出处:网络
Hi guys I am creating a simple website using the language of VB.NET, I am having trouble with one part at the moment and could really use some help.

Hi guys I am creating a simple website using the language of VB.NET, I am having trouble with one part at the moment and could really use some help.

At the moment I have items that are stored in a database, when the homepage loads these items are added to a Gridview which allows the user to select the items they wish to add to their shopping cart. Once an item is selected from the gridview it is added to a collection. That collection is then added to a Session. This is the code I have for completing that task.

  Public Function addToCollection() As Collection
    If Session("Order") Is Nothing Then
        colOrder = New Collection
        Session("Order") = colOrder
    Else
        colOrder = Session("Order")
    End If
    Return colOrder
End Function

Then this code will fill the session with the collection of items

    addToCollection()
    Dim gvRow As GridViewRow = gvCDs.SelectedRow
    Dim objOrder As Order = New Order
    objOrder.ID = gvRow.Cells(1).Text
    objOrder.Title = gvRow.Cells(2).Text
    objOrder.Artist = gvRow.Cells(3).Text
    objOrder.Price = gvRow.Cells(5).Text
    colOrder.Add(objOrder)

    Session("Order") = colOrder

Now on a new page I will display what items the user has stored in the Session by putting all the items in the session into a Gridview, This is the code for doing that

  gvOrder.DataSource = Session("Order")
  gvOrder.DataBind()

Now this is the part where I am stuck on and need someone to help me out, I want the user to be able to select a row in the gridview by using the gridviews build in selected toll and then they can click a button that will delete that item from the session开发者_运维百科 and gridview.

Please can someone help me out with this. Thanks Tim


Make sure you have a column with CommandName = "Delete", then handle the RowDeleting event. This is the body of that event handler. I've added extra steps to make it more clear what's going on.

Dim orders as Collection = DirectCast(Session("Order"), Collection)
orders.RemoveAt(e.RowIndex)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号