2013-04-06 49 views
0

我想在後端刪除一個gridview行。當我單擊delete時,它將從gridview中刪除,但它不會從數據庫中刪除。任何人都可以看到爲什麼這可能是?我的代碼如下:從後端c的gridview中刪除一行c#

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(); 
} 

GridView的是:

<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> 

回答

2

看來你的查詢是不正確

DELETE * FROM Tbl_Booking WHERE [email protected]_PK 

用這個代替

DELETE FROM Tbl_Booking WHERE [email protected]_PK 

取出@從這個李的符號NE

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

然後調用顯式刪除對數據源

... 
SDSStudents.ConnectionString = connstring; 
SDSStudents.Delete(); 
GridView1.DataSource = SDSStudents; 
... 
+0

我已經改變它,但它仍然不起作用 – Claire

+0

@Claire你可以顯示Gridview嗎? –

+0

是的,我編輯了原來的帖子。 – Claire

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,你需要確保,你的數據綁定字段是在TableCellCollection指數1

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

因爲在你的情況下,刪除按鈕將在你的databound字段之前生成。所以刪除按鈕將在索引0 &您的databound字段* BookingID_PK *將在索引1.