Link to home
Start Free TrialLog in
Avatar of MSSexpert
MSSexpert

asked on

Gridview Control Edit Mode requires Pressing Edit Button Twice

Hi all..

I have a GridView control in an ASP.NET 2.0 page. I added a column that
contains edit, update, cancel buttons. Because I did that, I had to handle
RowEditing event or an exception was thrown.

Well... this is the body of the RowEditing event:

protected void gvParametros_RowEditing(object sender, GridViewEditEventArgs
e)
{
gvParametros.EditIndex = e.NewEditIndex;
}

The fact is that when page is loaded and I click the edit link, page is
refreshed but GridView doesn't enter edit mode. When I press edit link
again, GridView enters edit mode.

any help to solve this behavior would be greatly appreciated
Avatar of Justin_Case_77
Justin_Case_77

rebind the datagrid

protected void gvParametros_RowEditing(object sender, GridViewEditEventArgs
e)
{
gvParametros.DataBind()
gvParametros.EditIndex = e.NewEditIndex;
}
Avatar of MSSexpert

ASKER

Yes, you'd think that that is how it would work (rebinding).I am a total newbie at C#/ASP.Net BTW so be gentle with me.

I tried as you suggested and it calls "Page_PreRender" and the Gridview disappears from the screen when it exits the Page_PreRender Event. The Page.IsPostBack property is true.


        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        // Editing mode
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.DataBind();
            GridView1.EditIndex = e.NewEditIndex;
        }

        private void BindData()
        {
            GridView1.DataSource = dataExp;

            GridView1.DataMember = "EXPENSE";
            GridView1.DataBind();
        }
       
      protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindData();

        }
I changed the sequeence as suggested to:

      protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindData();

        }

No change in result (i.e. Grid still dissapeared)
ASKER CERTIFIED SOLUTION
Avatar of deanvanrooyen
deanvanrooyen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for that. The dataexp was being cleared so obviously things were a bit of a mess. I took a deep breath scrapped the entire class and rewrote the code and it worked first time. Thanks Again