Edit Mode in GridView not requiring two button clicks

Last post 09-19-2008 10:12 AM by ramireddyindia. 2 replies.

Sort Posts:

  • Edit Mode in GridView not requiring two button clicks

    09-19-2008, 10:01 AM
    • Member
      520 point Member
    • ahillyer
    • Member since 08-22-2004, 12:49 PM
    • Posts 275

    Hi all,

    I've used the GridView like a gazillion times but every time I used it in the past has been the same way - tied to a SqlDataSource using Stored Procedures.  This time I'm using a Shopping Cart stored in Session.  Essentially, I've written a sub to convert the cart into a DataTable and I'm binding the GridView to that DataTable.  The problem is that when I click on the Edit Button (whether I use AutoGenerateEditButton or I create my own) it doesn't go into Edit mode.  But when I click it a second time it does if I include GridViewName.EditIndex = e.NewEditIndex in the RowEditing Sub.  The same thing is true with the Cancel button. The delete button code is working fine.  Obviously, it functions differently when a SqlDatasource is tied to it versus a DataTable.  Any help would be appreciated.  Here is my code:

     

    <asp:GridView runat="server" ID="gvCart" AutoGenerateColumns="false"  CssClass="gv" Width="560px"
            BorderColor="#663300" BorderStyle="Solid" BorderWidth="1" DataKeyNames="EventID"
            HeaderStyle-BorderColor="#663300" HeaderStyle-BorderStyle="Solid" HeaderStyle-BorderWidth="1"
            RowStyle-BorderColor="#663300" RowStyle-BorderStyle="Solid" RowStyle-BorderWidth="1"
            AlternatingRowStyle-BorderColor="#663300" AlternatingRowStyle-BorderStyle="Solid"
            AlternatingRowStyle-BorderWidth="1" AutoGenerateEditButton="false" AutoGenerateDeleteButton="false"
            OnRowEditing="gvCart_RowEditing" OnRowCancelingEdit="gvCart_RowCancelingEdit">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:LinkButton runat="server" ID="lbEdit" Text="Edit" CommandName="Edit" />
                            <br />
                            <asp:LinkButton runat="server" ID="lbDelete" Text="Delete" CommandName="Delete" />
                            <ajax:ConfirmButtonExtender runat="server" ID="cbeDelete" TargetControlID="lbDelete"
                                 ConfirmText="Are you sure you want to remove this event from your shopping cart?" />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:LinkButton runat="server" ID="lbUpdate" Text="Update" CommandName="Update" />
                            <br />
                            <asp:LinkButton runat="server" ID="lbCancel" Text="Cancel" CommandName="Cancel" />
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField HeaderText="EventID" DataField="EventID" Visible="false" />
                    <asp:TemplateField HeaderText="Event">
                        <ItemTemplate>
                            <asp:Label runat="server" ID="lblEvent" Text='<%# Eval("Event") %>' />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Label runat="server" ID="lblEvent" Text='<%# Eval("Event") %>' />
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Description">
                        <ItemTemplate>
                            <asp:Label runat="server" ID="lblDescription" Text='<%# Eval("Description") %>' />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Label runat="server" ID="lblDescription" Text='<%# Eval("Description") %>' />
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Location">
                        <ItemTemplate>
                            <asp:Label runat="server" ID="lblLocation" Text='<%# Eval("Location") %>' />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Label runat="server" ID="lblLocation" Text='<%# Eval("Location") %>' />
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Date/Time">
                        <ItemTemplate>
                            <asp:Label runat="server" ID="lblDateTime" Text='<%# Eval("DateTime") %>' />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Label runat="server" ID="lblDateTime" Text='<%# Eval("DateTime") %>' />
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Cost">
                        <ItemTemplate>
                            <asp:Label runat="server" ID="lblCost" Text='<%# Eval("Cost") %>' />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Label runat="server" ID="lblCost" Text='<%# Eval("Cost") %>' />
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Qty" ItemStyle-HorizontalAlign="Center">
                        <ItemTemplate>
                            <asp:Label runat="server" ID="lblQuantity" Text='<%# Eval("Quantity") %>' />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox runat="server" ID="txtQuantity" MaxLength="3" Width="30px"
                                Text='<%# Bind("Quantity") %>' />
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <%--<asp:BoundField HeaderText="Qty" DataField="Quantity" ItemStyle-HorizontalAlign="Center" />--%>
                    <asp:TemplateField HeaderText="Total">
                        <ItemTemplate>
                            <asp:Label runat="server" ID="lblTotal" Text='<%# Eval("Total") %>' />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Label runat="server" ID="lblTotal" Text='<%# Eval("Total") %>' />
                        </EditItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
    Here is the codebehind:
    Protected Sub gvCart_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvCart.RowEditing
            gvCart.EditIndex = e.NewEditIndex
    End Sub
    
    Protected Sub gvCart_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gvCart.RowCancelingEdit
            gvCart.EditIndex = -1
    End Sub
     
  • Re: Edit Mode in GridView not requiring two button clicks

    09-19-2008, 10:11 AM
    Answer
    • All-Star
      86,764 point All-Star
    • ecbruck
    • Member since 12-30-2005, 7:39 PM
    • Des Moines, IA
    • Posts 9,209
    • Moderator

    Within the RowEditing event handler you need to re-bind your GridView to the DataTable once again after setting the EditIndex.

    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

  • Re: Edit Mode in GridView not requiring two button clicks

    09-19-2008, 10:12 AM
    Answer

    in rowediting event, after set the editindex, again bind the data to gridview.

    gvCart.EditIndex = e.NewEditIndex
    gvcart.datasoruce = // your datasource;
    gvcart.databind() 

     

    similary do in rowcancelling event also, , again bind the data to gridview.

    Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
Page 1 of 1 (3 items)