Asp.net 在运行时从gridview动态删除行

Asp.net 在运行时从gridview动态删除行,asp.net,gridview,grid,Asp.net,Gridview,Grid,我创建了一个gridview,它通过项目模板添加了一个删除按钮。我想知道如何根据他单击的按钮从网格视图中动态删除行 protected void RemoveBtn_OnClick(object sender, EventArgs e) { Button clickedButton = sender as Button; GridViewRow row = (GridViewRow)clickedButton.Parent.Parent;

我创建了一个gridview,它通过项目模板添加了一个删除按钮。我想知道如何根据他单击的按钮从网格视图中动态删除行

protected void RemoveBtn_OnClick(object sender, EventArgs e)
    {
        Button clickedButton = sender as Button;
        GridViewRow row = (GridViewRow)clickedButton.Parent.Parent;
        int rowID = Convert.ToInt16(row.RowIndex);

        GridView1.DeleteRow(rowID);
    }



<asp:GridView ID= "GridView1" runat="server" AutoGenerateColumns="true" OnRowDeleting="RowDeletingEvent">
<Columns>
    <asp:TemplateField HeaderText="Remove Items">
        <ItemTemplate>
            <asp:Button id="RemoveBtn"  runat="server" Text="Remove" OnClick="RemoveBtn_OnClick"/>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>
protectedvoid RemoveBtn\u OnClick(对象发送方,事件参数e)
{
按钮clickedButton=发送者为按钮;
GridViewRow行=(GridViewRow)clickedButton.Parent.Parent;
int rowID=Convert.ToInt16(row.RowIndex);
GridView1.DeleteRow(rowID);
}
期待您的光临

请使用

当 在GridView中单击按钮 控制这使您能够提供 执行以下操作的事件处理方法: 每当发生此事件时,自定义例程 发生

GridView控件中的按钮可以 还可以调用一些内置的 控件的功能。到 执行以下操作之一,设置 按钮的CommandName属性 设置为下列值之一: 桌子

CommandName Value
"Delete" - Deletes the current record. Raises the RowDeleting and RowDeleted events.