Gridview remains in EDIT mode and doesn't get out of it when i don't rebind it, why ? but when i rebind gridview that it exits out of eidt mode successfully, why ?

    protected void gvTest_Edit(Object sender, GridViewEditEventArgs e) 
    {
        gvTest.EditIndex = e.NewEditIndex;
        Response.Write("Editing...");
        connectToDb(); // a funtion to rebind data but when i comment it then after click cancel button a gridview doesn't get out of EDIT mode else do.
    }

I think you may need to provide additional details. I have a general idea of what you have been working on, but I'm stumped at the moment on this one. There are other members here that may be able to assist but I think more info about the scenario may be helpful.

commented: ok , On it, see below +2

see jorGem, i created button in gridview to edit data, When i click that button then it calls

 gvTest_Edit method

tranforming a required row into edit mode bcz of

 gvTest.EditIndex = e.NewEditIndex;

but lets suppose i want to cancel EDITING by clicking CANCEL then calls this function:

protected void gvTest_EditCancel(Object sender, GridViewCancelEditEventArgs e) 
    {
        gvTest.EditIndex = -1;
      //  connectToDb();
        Response.Write("<br/>"+ "Editing cancelled");
    }

now in this function if i comment 'connectToDb()' then my GV doesn't come out of EDIT mode even after clicking CANCEL btn but the message appears which in mentioned

Response.Write("<br/>"+ "Editing cancelled");

but actually doesn't go out of edit mode but if i don't comment and let connectToDb() to be executed then everything works fine, (Y)
so what does it have to do with rebinding ?

i guess that it is because of VIEWSTATE, when cancelling event triggers the CANCEL function (without re binding control) then it restores old state of GV from VIEWSTATE and put it in browser like every server control but when it is being rebinded then it load again the data from source like a new instance of gridview control. i guess ? right guys ?

you tell it to cancel from the edit event by calling e.Cancel = true. This allows you to cancel edit mode when the edit was successful or leave it in edit mode if the edit failed.

protected void gvTest_Edit(Object sender, GridViewEditEventArgs e) 
{
    gvTest.EditIndex = e.NewEditIndex;
    Response.Write("Editing...");

    if (successful)
    {
        e.Cancel = true;
    }
    else
    {
        // Display a message or whatever you want to do
        // to let the user know there was a problem
    }
}

Cancel is a property of GridViewEditEventArgs, you can get more information on it from MSDN or by googling it I'm sure. When you are using a datasource control, such as SqlDatasource or EntityDatasource, the Cancel property is set automatically. When you are manually binding the gridview control then you have to tell it when to Cancel.

If you need to cancel within the CancelEdit event you need to do the same thing, set e.Cancel = true.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.