Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 618
  • Last Modified:

deleting rows from a gridview in asp.net, by obtaining the uniqueid.

I am trying to get the value of a bookingID from a asp.net gridview, so i can pass the value to a delete function, that will forward onto a sql stored procedure and delete the relevant row.

How can i obtain the bookingID as a selected row to pass it onto the function?
the grid view code is

<asp:GridView ID="SlotsBookedList" runat="server"  AutoGenerateColumns ="false"  AllowPaging ="true" DataKeyNames="BookingID">
          <columns>
         
               <asp:BoundField HeaderText = "student name" DataField = "username" />
               <asp:BoundField HeaderText = "Date" DataField = "BookingDate" />
               <asp:BoundField HeaderText = "Name of Machine" DataField = "MachineName" />
               <asp:BoundField HeaderText = "Slot Time Booked" DataField = "SlotTime" />
               <asp:BoundField HeaderText = "Slot Time Booked" DataField = "BookingID"  Visible="False"/>
               
                  <asp:CommandField HeaderText="Configure" DeleteImageUrl="/images/Delete.gif" ShowDeleteButton="True"
                        ButtonType="Image" />
           
            </columns>
           
        </asp:GridView>

.

The vb code to action thus far is:

Protected Sub SlotsBookedList_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles SlotsBookedList.RowDeleting

        Dim s As String

       'ctb.DeleteBookings("bookingID")

       
        Me.Label1.Text = s




    End Sub

I can return the rowIndex no problem using s = e.RowIndex.ToString()
But i want the actual bookingID that is bound to the gridview.

Any clues?

Julia
0
Juliafrazer
Asked:
Juliafrazer
  • 2
  • 2
1 Solution
 
surajgupthaCommented:
Have you tried this?
grd.Rows[selInd].Cells["id"].Value
0
 
JuliafrazerAuthor Commented:
Ive got to this stage, and it works in terms of deleting the rows, but i cannot rebind to the table to refresh.  An error message comes up.
Ive seen something on forums saying i should use (int) in front of bookingID = Me.SlotsBookedList.DataKeys(e.RowIndex).Value
ie.bookingID = (int) Me.SlotsBookedList.DataKeys(e.RowIndex).Value.
But this doesnt work.

Any clues

  Protected Sub SlotsBookedList_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles SlotsBookedList.RowDeleting

        'finds the primary key id of the gridview selected row,so we can parse it to the procedure that deletes bookings
     

        Dim bookingID As Integer


        bookingID = Me.SlotsBookedList.DataKeys(e.RowIndex).Value

        ctb.DeleteBookings(bookingID)

     
     

        If Not Page.IsPostBack Then
            Me.SlotsBookedList.DataBind()

        End If

    End Sub
0
 
JuliafrazerAuthor Commented:
Ok ive found the solution.

Protected Sub SlotsBookedList_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles SlotsBookedList.RowDeleting

        'finds the primary key id of the gridview selected row,so we can parse it to the procedure that deletes bookings
        'Dim bookingID As Integer = Me.SlotsBookedList.DataKeys(e.RowIndex).Value

        Dim bookingID As Integer


        'gets the bookingid from the selected row
        bookingID = Me.SlotsBookedList.DataKeys(e.RowIndex).Value

        'passes the bookingid to the deleting storedprocedure in the database
        ctb.DeleteBookings(bookingID)

        'rebinds the data to the table, ie. with deleted row
        Me.SlotsBookedList.DataBind()

        'reloads the page, to show the newly updated table, without re-pulling from the database
        Response.Redirect("StudentUnbook.aspx")




    End Sub

The redirect to the page, reloads the first procedure which repopulates the table.
0
 
surajgupthaCommented:
Cool. Best of luck :)
0
 
Computer101Commented:
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now