0

I have multiple gridviews on my webpage which are dynamically created. I need to add delete button to all the gridviews. on click of the button i need to delete the row in that particular grid.

I have added delete button to the grid. on click of the button i need to get the grid id so that i can delete the row in that grid. How can this be achieved.

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);
}

How to get the gridview id to delete rows

I am generating grid for each table. When i click on delete , it should point to that particular grid and delete the row. I have worked on static gridview. but handling events in dynamic grid, i am lost.

Nirav Vasoya
  • 356
  • 3
  • 18
Aswini
  • 51
  • 4
  • 16
  • I would refactor this by having a `Repeater` which contains a `GridView`. Then bind the repeater to a datasource that gets you as many grids as you want. This way, you can let the web forms framework handle the event registrations, raising the events and so on. With dynamic controls, *you* have to do all that work and it is trickier than you might expect. That said, if you're absolutely determined to make it work with dynamic controls, you should move the control creation/initialization to the `Page_PreInit` method. See (for instance) [here](https://stackoverflow.com/a/23298772/1429080) – user1429080 Apr 30 '19 at 08:54

1 Answers1

0

Check your sender object in debug. You will find grid ID in sender object.

Aamir Nakhwa
  • 393
  • 1
  • 12
  • i am trying to do that but grid.RowDeleting += new GridViewDeleteEventHandler(OnRowDeleting); event is not firing on button click – Aswini Apr 30 '19 at 08:17
  • Alright, I thought event is attached with your dynamic grids. Try to attach some other event and see if it work... – Aamir Nakhwa Apr 30 '19 at 08:20
  • yes event is attached with dynamic grids. for which event should i try – Aswini Apr 30 '19 at 08:24
  • It can be any event, try RowCommand. It's just to check something wrong with RowDeleting event or none of events are working on your grid. – Aamir Nakhwa Apr 30 '19 at 08:26
  • i now get the deleteeventhandler fired. Now i am getting my gridview id also. how will i delete row from the grid now. is there any direct way to do that – Aswini Apr 30 '19 at 08:53
  • You can remove that record from the datasource of the grid and set the datasource of the grid again. By the way what did you change to trigger the event? – Aamir Nakhwa Apr 30 '19 at 09:23