Juliafrazer
asked on
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/De lete.gif" ShowDeleteButton="True"
ButtonType="Image" />
</columns>
</asp:GridView>
.
The vb code to action thus far is:
Protected Sub SlotsBookedList_RowDeletin g(ByVal sender As Object, ByVal e As System.Web.UI.WebControls. GridViewDe leteEventA rgs) Handles SlotsBookedList.RowDeletin g
Dim s As String
'ctb.DeleteBookings("booki ngID")
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
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/De
ButtonType="Image" />
</columns>
</asp:GridView>
.
The vb code to action thus far is:
Protected Sub SlotsBookedList_RowDeletin
Dim s As String
'ctb.DeleteBookings("booki
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
ASKER
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.DataKey s(e.RowInd ex).Value
ie.bookingID = (int) Me.SlotsBookedList.DataKey s(e.RowInd ex).Value.
But this doesnt work.
Any clues
Protected Sub SlotsBookedList_RowDeletin g(ByVal sender As Object, ByVal e As System.Web.UI.WebControls. GridViewDe leteEventA rgs) Handles SlotsBookedList.RowDeletin g
'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.DataKey s(e.RowInd ex).Value
ctb.DeleteBookings(booking ID)
If Not Page.IsPostBack Then
Me.SlotsBookedList.DataBin d()
End If
End Sub
Ive seen something on forums saying i should use (int) in front of bookingID = Me.SlotsBookedList.DataKey
ie.bookingID = (int) Me.SlotsBookedList.DataKey
But this doesnt work.
Any clues
Protected Sub SlotsBookedList_RowDeletin
'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.DataKey
ctb.DeleteBookings(booking
If Not Page.IsPostBack Then
Me.SlotsBookedList.DataBin
End If
End Sub
ASKER
Ok ive found the solution.
Protected Sub SlotsBookedList_RowDeletin g(ByVal sender As Object, ByVal e As System.Web.UI.WebControls. GridViewDe leteEventA rgs) Handles SlotsBookedList.RowDeletin g
'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.DataKey s(e.RowInd ex).Value
Dim bookingID As Integer
'gets the bookingid from the selected row
bookingID = Me.SlotsBookedList.DataKey s(e.RowInd ex).Value
'passes the bookingid to the deleting storedprocedure in the database
ctb.DeleteBookings(booking ID)
'rebinds the data to the table, ie. with deleted row
Me.SlotsBookedList.DataBin d()
'reloads the page, to show the newly updated table, without re-pulling from the database
Response.Redirect("Student Unbook.asp x")
End Sub
The redirect to the page, reloads the first procedure which repopulates the table.
Protected Sub SlotsBookedList_RowDeletin
'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.DataKey
Dim bookingID As Integer
'gets the bookingid from the selected row
bookingID = Me.SlotsBookedList.DataKey
'passes the bookingid to the deleting storedprocedure in the database
ctb.DeleteBookings(booking
'rebinds the data to the table, ie. with deleted row
Me.SlotsBookedList.DataBin
'reloads the page, to show the newly updated table, without re-pulling from the database
Response.Redirect("Student
End Sub
The redirect to the page, reloads the first procedure which repopulates the table.
Cool. Best of luck :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
grd.Rows[selInd].Cells["id