How to create gridview updating event dynamically?

Total Post:154

Points:1086
 1077  View(s)
Ratings:
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();
        da.Fill(ds);
        if(ds.Tables[0].Rows[0] != null)
        {
            GridView1.AutoGenerateEditButton = true;
            GridView1.DataSource = ds;
            GridView1.DataBind();
            GridView1.RowUpdating += new GridViewUpdateEventHandler(abc);
            GridView1.DataKeyNames = new string[] { "id" };
            GridView1.RowEditing += new GridViewEditEventHandler(bc);
        }   
        else
            Response.Write("fkj");
    }
    protected void abc(object sender, GridViewUpdateEventArgs e)
    {
        Response.Write(e.RowIndex);
    }
    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. Post:378

    Points:2694
    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">

    </asp:GridView>

    You need to add:

    OnRowEditing="nameOfMethodYouWantToFire"

    so it looks like:

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

    </asp:GridView>

    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

      }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!