2016-06-24 65 views
0

我有一個系統,將項目添加到DataGrid中,我的問題是我如何使用刪除按鈕?當用戶想要刪除該項目時,用戶將按下按鈕以刪除該行,但我似乎無法使其工作。謝謝!如何刪除gridview行?而數據還沒有保存在數據庫中

<asp:GridView runat="server" ID="gridview" CssClass="table-hover" AutoGenerateColumns="true" HeaderStyle-BackColor="CornflowerBlue" BackColor="White" BorderWidth="5" BorderColor="CornflowerBlue" OnSelectedIndexChanged="gridview_SelectedIndexChanged" CellPadding="8" 
    CellSpacing="0" Width="100%" OnRowDeleting="gridview_RowDeleting" EmptyDataText="No records to display"> 
    <HeaderStyle BackColor="CornflowerBlue"></HeaderStyle> 
    <Columns> 
     <asp:CommandField ShowDeleteButton="true" ButtonType="Button" /> 
     <asp:TemplateField ItemStyle-Width="25px" HeaderText=""> 
      <ItemTemplate> 
       <asp:ImageButton ID="lnkEdit" runat="server" ImageUrl="~/Images/Icons/Modify.png" OnClick="Edit" /> 
       <%--<asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" OnClick="Edit"></asp:LinkButton>--%> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

這裏是我的腳本

<script runat="server"> 

    void gridview_RowDeleting(object sender, GridViewDeleteEventArgs e) 
    { 
     TableCell cell = gridview.Rows[e.RowIndex].Cells[2]; 
    } 
</script> 

grid view picture

回答

0

在GridView的添加OnRowCommand="gridview_RowCommand"

CommandField中可以工作,但我是這樣的:

<asp:TemplateField ItemStyle-Width="25px" HeaderText=""> 
     <ItemTemplate> 
      <asp:Button ID="lnkDel" runat="server" Text="Delete" CommandName="Del" CommandArgument='<%#Eval("ID")%>' /> 
     </ItemTemplate> 
    </asp:TemplateField> 

在代碼後面

protected void gridview_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    if (e.CommandName == "Del") { 
      //Get Command Argument 
      int IdToDelete = Convert.ToInt32(e.CommandArgument.ToString()); 
      //Your Delete Command 
      //Rebind GridView 
     } 
} 
+0

很高興幫助你,如果是這種情況,請投票。謝謝 – Sami

相關問題