以编程方式将OnRowDelete添加到GridView

时间:2016-04-04 15:16:59

标签: asp.net gridview commandfield

我以编程方式生成GridView个,GridViewCommandField有一个删除按钮。如何以编程方式添加OnDeletingRow,以便在单击带有GridView的删除按钮时调用函数?

                    GridView gv = new GridView();
                    gv.AutoGenerateColumns = false;
                    gv.ID = "GridView_" + selectedId;
                    gv.DataKeyNames = new string[] {"id"};
                    gv.AllowPaging = false;
                    gv.CellPadding = 3;
                    gv.RowDeleting += new GridViewDeleteEventHandler(gv_RowDeleting);

                    CommandField commandfieldDeallocate = new CommandField();
                    commandfieldDeallocate.HeaderText = "DELETE NUMBER";
                    commandfieldDeallocate.ShowDeleteButton = true;
                    commandfieldDeallocate.DeleteText = "DELETE";
                    gv.Columns.Add(commandfieldDeallocate);

1 个答案:

答案 0 :(得分:0)

在C#中,你可以这样做:

gv.RowDeleting += new GridViewDeleteEventHandler(gv_RowDeleting);

void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    // Perform deletion
}