0

我正在尝试删除后端的 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>
4

2 回答 2

2

似乎您的查询不正确

DELETE * FROM Tbl_Booking WHERE ID=@BookingID_PK

改用这个

DELETE FROM Tbl_Booking WHERE ID=@BookingID_PK

@从此行 中删除符号

SDSBooking.DeleteParameters.Add("BookingID_PK",...); 

然后针对数据源显式调用 Delete

... 
SDSStudents.ConnectionString = connstring;
SDSStudents.Delete();
GridView1.DataSource = SDSStudents;  
...
于 2013-04-06T21:46:29.300 回答
0

您从数据库中删除记录的代码序列应该是这样的。

//Declaring the datasource.
SqlDataSource SDSBooking= new SqlDataSource();

//Providing the delete command.
SDSBooking.DeleteCommand = "DELETE FROM Tbl_Booking WHERE BookingID_PK = @BookingID_PK";

//Adding the parameter for deleting the record.
SDSBooking.DeleteParameters.Add("BookingID_PK", GVMyBookings.Rows[e.RowIndex].Cells[1].Text);

//Providing the connection string.
SDSBooking.ConnectionString = connstring;

//Executing the delete method of the SqlDataSouce. 
//It is this line that will actually delete your record.
SDSBooking.Delete();

在此之后,您可以将此数据源分配给您的 gridview。

注意:对于GVMyBookings.Rows[e.RowIndex].Cells[1].Text,您需要确保您的数据绑定字段位于1TableCellCollection 中的索引处。

如果您在它之前生成一些其他列,您可能需要更改。

因为在您的情况下,删除按钮将在您的数据绑定字段之前生成。因此,删除按钮将位于索引 0,而您的数据绑定字段 *BookingID_PK* 将位于索引 1。

于 2013-04-08T06:18:55.177 回答