sabrine
sabrine

Reputation: 21

Edit, Delete and Insert in a programmatically created GridView

So I'm facing a problem. In fact i have created a databound GridView programmatically. The problem is I want to be interactive (update , delete, insert and all that drill) but only using c# since i have created it programmatically. Here is the code that'ive created with the GridView

 GridView grid = new GridView();
            //CSS
            grid.CssClass = "grid";
            grid.ForeColor = System.Drawing.Color.Gray;
            grid.ShowFooter = true;
            grid.AlternatingRowStyle.CssClass = "gridAltRow";
            grid.RowStyle.CssClass = "gridRow";
            grid.FooterStyle.CssClass = "gridFooterRow";
            grid.DataKeyNames = new string[] { "ID" };
            grid.RowCommand




            //End Css

            grid.AutoGenerateEditButton = true;            

            grid.DataSource = element.GetElementByRubric(testrub.ID);

            panel.Controls.Add(grid);
            grid.DataBind();

            TabContainer1.Tabs.Add(panel);

as you can see i added an edit button but it does nothing of course. Does anyone have any suggestions ??

thx

Upvotes: 0

Views: 2502

Answers (3)

chead23
chead23

Reputation: 1879

You need to add a handler for the OnRowEditing event. Something similar to

grid.RowEditing+=new GridViewEditEventHandler(grid_RowEditing_RowEditing);

to attach the event handler and then an event handler like below. In this case the sender will be the dynamically created GridView.

protected void grid_RowEditing(object sender, GridViewEditEventArgs e)
{
   ((GridView)sender).EditIndex = e.NewEditIndex;
   // Then call your databinding method here to update the grid.
}

This would also be the same method you would use to add handlers for the delete and insert events also.

Upvotes: 0

Spencer Ruport
Spencer Ruport

Reputation: 35117

Why are you generating a grid in code? This is highly unusual.

It may or may not be possible but why are you avoiding just writing the grid directly to the ASPX file?

Upvotes: 0

Anish John
Anish John

Reputation: 410

You have to write the function for updating the database in onrowupdating event of the gridview to make the edit link work.

Upvotes: 1

Related Questions