Madam Zu Zu
Madam Zu Zu

Reputation: 6605

Cancel row command does not work for GridView

i have a gridview which is inside an update panel. when i click "cance" after the edit, nothing happens. When i debug it does go into my gvWorkhours_RowCommand function and inside the cancel if, but nothing happens on screen (the edit fields are all still visible)

here is what i have:

<asp:GridView ID="gvWorkhours" runat="server" AutoGenerateColumns="false" CssClass="GridViewStyle" OnRowEditing="gvWorkhours_RowEditing"  
 OnRowCommand="gvWorkhours_RowCommand"  >
<EmptyDataTemplate>
no data returned
</EmptyDataTemplate>
<Columns>
<asp:commandfield buttontype="Link" showeditbutton="true" edittext="Edit" />
     <asp:TemplateField HeaderText="Organization">
        <ItemTemplate>
            <%# Eval("org.orgCode").ToString() + "- " + Eval("org.orgSubCode").ToString()%>
        </ItemTemplate>
    </asp:TemplateField>
 <asp:TemplateField HeaderText="Year-Qtr">
        <ItemTemplate>
            <%# Eval("year").ToString() + "- " + Eval("qtr").ToString()%>
        </ItemTemplate>
    </asp:TemplateField>
.......






 protected void gvWorkhours_RowEditing(Object sender, GridViewEditEventArgs e)
    {
        populateGrid();  //pulls from the Db and binds to the grid
        gvWorkhours.EditIndex = e.NewEditIndex;  //This is the selected row to edit
        gvWorkhours.DataBind();  //Make the edit Template show up

    }

    protected void gvWorkhours_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        // If multiple buttons are used in a GridView control, use the
        // CommandName property to determine which button was clicked.
        if (e.CommandName == "Cancel")
        {
            gvWorkhours.EditIndex = -1;
            populateGrid(); 
        }

    }





private void populateGrid()
    {
        //getting all variables for update here.
            Workhours wh = new Workhours(selectedItem, year, qtr);
            gvWorkhours.DataSource = wh.exposures;
            gvWorkhours.DataBind();
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
            //  throw(ex);
        }
    }

what am i missing here?

Upvotes: 2

Views: 2703

Answers (3)

Greg
Greg

Reputation: 746

Yes it's an old question, but no answer.

Cancel needs to be in a RowCancelEdit method ... seems 'Cancel' is a protected word in this scenario

        protected void gvResults_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView gv = (GridView)sender;
            gv.EditIndex = -1;
            BindGrid2();
        }

Upvotes: 0

Md. Parvez Alam
Md. Parvez Alam

Reputation: 4596

try to give other word than cancel like "CancelRecord" and then try with it

Upvotes: 0

Ann L.
Ann L.

Reputation: 13975

You don't have the UpdatePanel shown in your example, so I don't know how you have it set up, but if you have the UpdateMode set to Conditional, you probably need to manually call the Update method on the update panel:

    if (e.CommandName == "Cancel")
    {
        gvWorkhours.EditIndex = -1;
        populateGrid(); 
        UpdatePanel1.Update();   // whatever the name of the UpdatePanel is
    }

Upvotes: 1

Related Questions