我正在尝试删除后端的 gridview 行。当我单击delete
它时,它会从 中删除gridview
,但它不会从数据库中删除。谁能明白为什么会这样?我的代码如下:
protected void GVMyBookings_DeleteBooking(object sender, GridViewDeleteEventArgs e)
{
string connstring = ConfigurationManager.ConnectionStrings["BookingConn"].ToString();
SqlConnection MyConnection = new SqlConnection(connstring);
MyConnection.Open();
SqlDataSource SDSBooking= new SqlDataSource();
SDSBooking.DeleteCommand = "DELETE FROM Tbl_Booking WHERE BookingID_PK = @BookingID_PK";
SDSBooking.DeleteParameters.Add("@BookingID_PK", GVMyBookings.Rows[e.RowIndex].Cells[0].ToString());
SDSBooking.ConnectionString = connstring;
GVMyBookings.DataSource = SDSBooking;
GVMyBookings.DataBind();
MyConnection.Close();
}
网格视图是:
<asp:GridView ID="GVMyBookings" runat="server" GridLines="Vertical" AllowSorting="True"
AutoGenerateColumns="False" AutoGenerateDeleteButton="true"
OnRowDeleting="GVMyBookings_DeleteBooking" EmptyDataText="You have no upcoming bookings" >
<RowStyle BackColor="#e5ecbf" />
<Columns>
<asp:BoundField DataField="BookingID_PK" />
<asp:BoundField DataField="BookingDate" HeaderText="Booking Date"
SortExpression="BookingDate" DataFormatString="{0:d}" />
<asp:BoundField DataField="RoomName" HeaderText="Room Name"
SortExpression="RoomName" />
<asp:BoundField DataField="StartTime" HeaderText="Start Time"
SortExpression="StartTime"/>
<asp:BoundField DataField="EndTime" HeaderText="End Time"
SortExpression="EndTime" />
<asp:BoundField DataField="StaffUID" HeaderText="StaffUID"
SortExpression="StaffUID" Visible="false" />
</Columns>
<HeaderStyle BackColor="#264409" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>