在c#asp.net中动态创建的gridview中删除行

时间:2019-04-30 06:40:37

标签: c# asp.net gridview dynamic delete-row

我的网页上有多个动态创建的网格视图。我需要将删除按钮添加到所有gridviews。在单击按钮时,我需要删除该特定网格中的行。

我已将删除按钮添加到网格中。在单击按钮时,我需要获取网格ID,以便可以删除该网格中的行。如何做到这一点。

protected GridView generategrid(int counter)
{
    //creating grid from code behind
    GridView grid = new GridView();
    grid.AutoGenerateColumns = false;

    BoundField techname = new BoundField();
    techname.HeaderText = "Tech Name";
    techname.DataField = "Tech Name";
    grid.Columns.Add(techname);

    BoundField name = new BoundField();
    name.HeaderText = "Name";
    name.DataField = "Name";
    grid.Columns.Add(name);

    TemplateField Action = new TemplateField();
    Action.HeaderText = "Action";
    Action.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
    grid.Columns.Add(Action);

    grid.RowCreated += deleteButton;
    grid.ID = "gv_conditiontable" + (counter + 1);

    grid.RowDeleting += new 
    GridViewDeleteEventHandler(OnRowDeleting);
}

protected void Page_Load(object sender, EventArgs e)
{
     for (int i = 0; i < Tablecount; i++)
     {          
         generategrid(i);
     }
}

protected void deleteButton(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Button Btn = new Button();
        Btn.ID = "btn_selectcols";
        Btn.Text = "Delete";
        Btn.CommandName = "Delete";

        e.Row.Cells[2].Controls.Add(Btn);
    }     
}

protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
    int index = Convert.ToInt32(e.RowIndex);
}

如何获取gridview ID以删除行

我正在为每个表生成网格。当我单击delete时,它应指向该特定网格并删除该行。我从事过静态gridview的工作。但是在动态网格中处理事件,我迷路了。

1 个答案:

答案 0 :(得分:0)

在调试中检查发送方对象。您将在发送方对象中找到网格ID。