Advertise with us

How to create gridview updating event dynamically?

Posted by  Manoj Bhatt
 1373  View(s)
Rate this:
public partial class Gridvw_expt2 : System.Web.UI.Page
    SqlCommand com;
    SqlDataAdapter da;
    DataSet ds;
    SqlConnection con= new SqlConnection(ConfigurationManager.ConnectionStrings["gj"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
        com = new SqlCommand("Select * from tblExpt",con);
        da = new SqlDataAdapter(com);
        ds = new DataSet();
        if(ds.Tables[0].Rows[0] != null)
            GridView1.AutoGenerateEditButton = true;
            GridView1.DataSource = ds;
            GridView1.RowUpdating += new GridViewUpdateEventHandler(abc);
            GridView1.DataKeyNames = new string[] { "id" };
            GridView1.RowEditing += new GridViewEditEventHandler(bc);
    protected void abc(object sender, GridViewUpdateEventArgs e)
    protected void bc(object sender, GridViewEditEventArgs e)
        GridView gv =(GridView)sender;
        gv.EditIndex =e.NewEditIndex;

the row used to get in edit mode only if i edit the next row means first row never get in edited mode.

  1. Re: How to create gridview updating event dynamically?

    Hi Manoj, 

    Its because you haven't set up a handler to handle GridView.RowEditing. On your gridview (in the .aspx) you need to wire up a method which will deal with RowEditing.

    You're gridview code will look like:

    <asp:GridView ID="GridView1" runat="server">


    You need to add:


    so it looks like:

    <asp:GridView ID="GridView1" runat="server" OnRowEditing="nameOfMethodYouWantToFire">


    Where nameOfMethodYouWantToFire is in your code behind (your C#) and it handles the event. Something like this:

    protected void nameOfMethodYouWantToFire(object sender, GridViewPageEventArgs e)


        //Your code here