2014-11-03 39 views
0

我怎麼能在我的datagridview刪除整行。我已經有一個刪除鏈接在我的DataGrid .. 這裏是VB如何刪除我的SQL數據視圖中的整行?

<asp:GridView ID="EmployeeHallway" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteButton="False" AutoGenerateSelectButton="True" Height="93px" HorizontalAlign="Center" PageSize="6" style="margin-bottom: 0px; text-align: center;" Width="768px"> 
       <AlternatingRowStyle BackColor="White" /> 
       <Columns> 
        <asp:TemplateField> 
         <ItemTemplate> 

           <%--ADD THE DELETE LINK BUTTON--%> 
           <asp:LinkButton ID="LinkButton2" Runat="server" OnClientClick ="return confirm('Are you sure you?');" 
            CommandName="Delete">Delete</asp:LinkButton> 

         </ItemTemplate> 
        </asp:TemplateField> 
        <asp:BoundField DataField="EmployeeID" HeaderText="Locker ID" /> 
        <asp:BoundField DataField="Name" HeaderText="Name" /> 
        <asp:BoundField DataField="EmployeeNo" HeaderText="EmployeeNo" /> 
        <asp:BoundField DataField="Email" HeaderText="Email" /> 
        <asp:BoundField DataField="Location" HeaderText="Location" /> 
       </Columns> 
       <HeaderStyle BackColor="#003366" BorderColor="#336699" BorderStyle="Solid" ForeColor="White" /> 
       <PagerStyle BackColor="#003366" BorderColor="#336699" ForeColor="White" /> 
       <RowStyle BackColor="White" ForeColor="#003366" /> 
       <SelectedRowStyle BackColor="White" ForeColor="#6600FF" /> 
       <SortedAscendingCellStyle BackColor="#CCCCCC" /> 
</asp:GridView> 

我的標記代碼,當我點擊刪除鏈接此錯誤顯示

「的GridView的 'EmployeeHallway' 解僱事件RowDeleting這是不處理的。」

誰能幫助我該怎麼請做下一步

+0

[GridView'PendingRecordsGridview'觸發的事件RowDeleting沒有被處理]的可能的重複(http://stackoverflow.com/questions/14301815/the-gridview-pendingrecordsgridview-fired-event-rowdeleting-which-wasnt -handl) – 2014-11-03 05:07:46

回答

1

您使用Delete作爲CommandName作爲刪除鏈接,因此它會自動創建RowDeleting事件。所以,你必須實現它是這樣的:

您必須添加如下​​事件:

<asp:GridView ID="EmployeeHallway" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteButton="False" AutoGenerateSelectButton="True" Height="93px" HorizontalAlign="Center" PageSize="6" style="margin-bottom: 0px; text-align: center;" Width="768px"> 
    <RowStyle ForeColor="#003399" HorizontalAlign="Center" OnRowDeleting="EmployeeHallway_RowDeleting"/> 

,並在後臺代碼:

Public Sub EmployeeHallway_RowDeleting(sender As Object, e As GridViewDeleteEventArgs) 

End Sub 
+0

糾正我,如果即時通訊錯誤,但此代碼是爲C#權利?我需要VB環境代碼先生。 – 2014-11-03 05:11:20

+0

哈哈得到它感謝Vishal – 2014-11-03 05:14:18

0

在aspx頁面添加OnRowDeleting="OnRowDeleting"

將您的DataTable保存在ViewState("dt")然後做像這樣:

Protected Sub OnRowDeleting(sender As Object, e As GridViewDeleteEventArgs) 
    Dim index As Integer = Convert.ToInt32(e.RowIndex) 
    Dim dt As DataTable = TryCast(ViewState("dt"), DataTable) 
    dt.Rows(index).Delete() 
    ViewState("dt") = dt 
    BindGrid() 
End Sub 

Protected Sub BindGrid() 
    EmployeeHallway.DataSource = TryCast(ViewState("dt"), DataTable) 
    EmployeeHallway.DataBind() 
End Sub 
+0

BindGrid()出現錯誤「BindGrid未聲明。」 – 2014-11-03 05:36:25

+0

發生此錯誤「未將對象引用設置爲對象的實例。」 – 2014-11-03 05:42:45

+0

是否將'DataTable'保存在ViewState(「dt」)中? – 2014-11-03 05:48:10

相關問題