Question

I have a gridview that adds rows in the footertemplate that works fine. I want to add RowEditing and RowDeleting to the grid. I added the RowEditing method but when I click the 'Edit' Button, the add row is displayed in the footer as if it is the "Add Row' command. When I created a prototype of a grid, I had a column just for editing and it was a commandfield. WHen you click the button, the row would change so that all of the fields were editable. How can I do that when the field is an ItemTemplate field?

This is my markup:

<asp:GridView ID="MappingGridView" runat="server" AllowSorting="True" AutoGenerateColumns="False" Caption="Enrollment Mapping Information" CaptionAlign="Top" 
    CssClass="grid" HorizontalAlign="Left" ShowFooter="True" AllowPaging="True" PageSize="4" ShowHeaderWhenEmpty="true" OnPageIndexChanging="MappingGridView_PageIndexChanging" 
     OnRowDataBound="MappingGridView_RowDataBound" OnRowCommand="MappingGridView_RowCommand" Width="1051px" OnRowEditing="MappingGridView_RowEditing">
    <Columns>
         <asp:TemplateField HeaderText="MappingID" SortExpression="mgvMappingID">
            <ItemTemplate>
                <asp:Label ID="mgvLblMappingID" runat="server" Text='<%# Bind("EnrollmentMappingID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="SectionID">
            <EditItemTemplate>
                <asp:TextBox ID="mgvDdlSectionID" runat="server" Text='<%# Bind("SectionID") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="mgvLblSectionID" runat="server" Text='<%# Bind("SectionID") %>'></asp:Label>
            </ItemTemplate>
             <FooterTemplate>
                <asp:TextBox ID="mgvInsertSectionID" runat="server" ReadOnly="true" Text="" Width="90%"></asp:TextBox>
            </FooterTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="SchoologyCourseID" SortExpression="mgvSchoologyCourseID">
            <EditItemTemplate>
                <asp:TextBox ID="mgvEditSchoologyCourseID" runat="server" Text='<%# Bind("SchoologyCourseID") %>'></asp:TextBox>
                 <asp:RequiredFieldValidator ID="ReqFieldValEditSchoologyCourseID" ControlToValidate="mgvEditSchoologyCourseID" runat="server" 
                     ErrorMessage="Required field." ValidationGroup="MappingGrid" Display="Dynamic"
                     CssClass="message-error">
                 </asp:RequiredFieldValidator>
                 <asp:RegularExpressionValidator ID="RegExpValEditSchoologyCourseID" ControlToValidate="mgvEditSchoologyCourseID" runat="server" 
                     ErrorMessage="Enter only integers." ValidationGroup="MappingGrid" ValidationExpression="^\d+$" 
                     Display="Dynamic" CssClass="message-error">
                 </asp:RegularExpressionValidator>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="mgvLblSchoologyCourseID" runat="server" Text='<%# Bind("SchoologyCourseID") %>'></asp:Label>
            </ItemTemplate>
             <FooterTemplate>
                <asp:TextBox ID="mgvInsertSchoologyCourseID" runat="server" Width="90%"></asp:TextBox>
                 <asp:RequiredFieldValidator ID="ReqFieldValSchoologyCourseID" ControlToValidate="mgvInsertSchoologyCourseID" runat="server" 
                     ErrorMessage="Required field." ValidationGroup="MappingGrid" Display="Dynamic"
                     CssClass="message-error">
                 </asp:RequiredFieldValidator>
                 <asp:RegularExpressionValidator ID="RegExpValSchoologyCourseID" ControlToValidate="mgvInsertSchoologyCourseID" runat="server" 
                     ErrorMessage="Enter only integers." ValidationGroup="MappingGrid" ValidationExpression="^\d+$" 
                     Display="Dynamic" CssClass="message-error">
                 </asp:RegularExpressionValidator>
            </FooterTemplate>
        </asp:TemplateField>

         <asp:TemplateField HeaderText="CECityActivityID" SortExpression="mgvCECityActivityID">
            <EditItemTemplate>
                <asp:TextBox ID="mgvEditCECityActivityID" runat="server" Text='<%# Bind("CECityActivityID") %>'></asp:TextBox>
                <asp:RegularExpressionValidator ID="RegExpValEditCECityID" ControlToValidate="mgvEditCECityActivityID" runat="server" 
                     ErrorMessage="Enter 0-9, A-F, and hyphens. Maximum length is 50." ValidationGroup="MappingGrid" ValidationExpression="^[0-9A-Fa-f-]{0,50}$" 
                     Display="Dynamic" CssClass="message-error">
                 </asp:RegularExpressionValidator>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="mgvLblCECityActivityID" runat="server" Text='<%# Bind("CECityActivityID") %>'></asp:Label>
            </ItemTemplate>
             <FooterTemplate>
                <asp:TextBox ID="mgvInsertCECityActivityID" runat="server" Width="90%"></asp:TextBox>
                <asp:RegularExpressionValidator ID="RegExpValCECityID" ControlToValidate="mgvInsertCECityActivityID" runat="server" 
                     ErrorMessage="Enter only 0-9, A-F, and hyphens; maximum length is 50." ValidationGroup="MappingGrid" ValidationExpression="^[0-9A-Fa-f-]{0,50}$" 
                     Display="Dynamic" CssClass="message-error">
                 </asp:RegularExpressionValidator>
            </FooterTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Is Active" SortExpression="mgvIsActive">
            <EditItemTemplate>
                <asp:DropDownList ID="mgvEditIsActive" runat="server" Text='<%# Bind("IsActive") %>'>
                    <asp:ListItem>Yes</asp:ListItem>
                    <asp:ListItem>No</asp:ListItem>
                </asp:DropDownList>                
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="mgvLblIsActive" runat="server" Text='<%# Bind("IsActive") %>'></asp:Label>
            </ItemTemplate>
             <FooterTemplate>
                <asp:DropDownList ID="mgvInsertIsActive" runat="server" Width="90%">
                    <asp:ListItem Selected="True">Yes</asp:ListItem>
                    <asp:ListItem>No</asp:ListItem>
                </asp:DropDownList>
            </FooterTemplate>
        </asp:TemplateField>

         <asp:TemplateField HeaderText="Max. Enrollment">
            <EditItemTemplate>
                <asp:TextBox ID="mgvEditMaxEnrollment" runat="server" Text='<%# Bind("MaxEnrollment") %>'></asp:TextBox>
                <asp:RegularExpressionValidator ID="RegExpValEditMaxEnroll" ControlToValidate="mgvEditMaxEnrollment" runat="server" 
                     ErrorMessage="Enter only integers." ValidationGroup="MappingGrid" ValidationExpression="^\d+$" 
                     Display="Dynamic" CssClass="message-error">
                </asp:RegularExpressionValidator>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="mgvLblMaxEnrollment" runat="server" Text='<%# Bind("MaxEnrollment") %>'></asp:Label>
            </ItemTemplate>
             <FooterTemplate>
                <asp:TextBox ID="mgvInsertMaxEnrollment" runat="server" Width="90%"></asp:TextBox>
                <asp:RegularExpressionValidator ID="RegExpValMaxEnroll" ControlToValidate="mgvInsertMaxEnrollment" runat="server" 
                     ErrorMessage="Enter only integers." ValidationGroup="MappingGrid" ValidationExpression="^\d+$" 
                     Display="Dynamic" CssClass="message-error">
                 </asp:RegularExpressionValidator>
            </FooterTemplate>
        </asp:TemplateField>

         <asp:TemplateField HeaderText="Action" ShowHeader="False">
            <EditItemTemplate>
                <asp:Button ID="mgvImgUpdate" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:Button>
                &nbsp;<asp:Button ID="mgvImgCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:Button>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Button ID="mgvEditButton" runat="server" CausesValidation="True" ValidationGroup="MappingGrid" CommandName="Edit" Text="Edit" Width="40%"></asp:Button>
                <asp:Button ID="mgvDeleteButton" runat="server" CausesValidation="false" CommandName="Delete" Text="Delete" Width="40%"></asp:Button>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" />
            <FooterTemplate>
                <asp:Button ID="mgvBtnAdd" runat="server" CommandName="Add" Text="Add Mapping" Width="90%" CausesValidation="true" ValidationGroup="MappingGrid" />
            </FooterTemplate>
        </asp:TemplateField>
     </Columns>
     <EmptyDataTemplate>
        <tr> 
            <td colspan="11" style="text-align:center;">
                No mapping data was found for this section. Mappings can be added to this section by clicking the 'Add Mapping' Button.
            </td>
        </tr>
        <tr>
            <td></td>
            <td><asp:TextBox ID="mgvInsertSectionID" runat="server" ReadOnly="true" Width="90%"></asp:TextBox></td>
            <td><asp:TextBox ID="mgvInsertSchoologyCourseID" runat="server" Width="90%"></asp:TextBox>
                <asp:RequiredFieldValidator ID="ReqFieldValSchoologyCourseIDEmpty" ControlToValidate="mgvInsertSchoologyCourseID" runat="server" 
                     ErrorMessage="Required field." ValidationGroup="MappingGrid" Display="Dynamic"
                     CssClass="message-error">
                 </asp:RequiredFieldValidator>
                 <asp:RegularExpressionValidator ID="RegExpValSchoologyCourseIDEmpty" ControlToValidate="mgvInsertSchoologyCourseID" runat="server" 
                     ErrorMessage="Enter only integers." ValidationGroup="MappingGrid" ValidationExpression="^\d+$" 
                     Display="Dynamic" CssClass="message-error">
                 </asp:RegularExpressionValidator>
            </td>
            <td><asp:TextBox ID="mgvInsertCECityActivityID" runat="server" Width="90%"></asp:TextBox>
                <asp:RegularExpressionValidator ID="RegExpValCECityIDEmpty" ControlToValidate="mgvInsertCECityActivityID" runat="server" 
                     ErrorMessage="Enter only 0-9, A-F, and hyphens; maximum length is 50." ValidationGroup="MappingGrid" ValidationExpression="^[0-9A-Fa-f-]{0,50}$" 
                     Display="Dynamic" CssClass="message-error">
                 </asp:RegularExpressionValidator>                  
            </td>
            <td><asp:DropDownList ID="mgvInsertIsActive" runat="server" Width="90%">
                <asp:ListItem Selected="True">Yes</asp:ListItem>
                <asp:ListItem>No</asp:ListItem>
                </asp:DropDownList></td>
            <td><asp:TextBox ID="mgvInsertMaxEnrollment" runat="server" Width="90%"></asp:TextBox>
                <asp:RegularExpressionValidator ID="RegExpValMaxEnrollEmpty" ControlToValidate="mgvInsertMaxEnrollment" runat="server" 
                     ErrorMessage="Enter only integers." ValidationGroup="MappingGrid" ValidationExpression="^\d+$" 
                     Display="Dynamic" CssClass="message-error">
                 </asp:RegularExpressionValidator>
            </td>
            <td><asp:Button ID="mgvBtnAddEmpty" runat="server" CommandName="Add" Text="Add Mapping" Width="90%" CausesValidation="true" 
                ValidationGroup="MappingGrid" />
            </td>
        </tr>
     </EmptyDataTemplate>
 </asp:GridView>

This is my code behind:

protected void MappingGridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        MappingGridView.EditIndex = e.NewEditIndex;
        populateEnrollMappingGrid();
    }

When I click the edit button, the RowEditing method is not called and the footer row displays the insert action.

How do I get the Edit button to make the row clicked editable?

UPDATED INFORMATION Page_Load This is the Page_Load Method:

 protected void Page_Load(object sender, EventArgs e)
    {          
        try
        {
            if (!IsPostBack)
            {
                //Get the Environment Setting to determine the database to access
                txtBoxEnvironment.Text = CurrentEnvironment;
                DAL.setCurrentEnvironment(CurrentEnvironment);

                //Get All Section information from database
                CSectionInfo.getData();

                //Get All Mapping informatio from database
                CEnrollMappingInfo.getData();

                //Populate the Mapping grid
                MappingGridView.DataSource = CEnrollMappingInfo.getInitialData();

                //Populate the Drop Down Section List
                populateDropDownSectionList(CEnrollMappingInfo.SelectedSectionID);

                //Grid must be bound after the Dropdown List is set so the value in the footer can be determined
                MappingGridView.DataBind();

                //Populate the Section grid
                SectionGridView.DataSource = CSectionInfo.getDataUsingSectionID(CEnrollMappingInfo.SelectedSectionID);

                //Get Course information from database
                populateCourseGrid();

                //Populate the Drop Down Course List
                populateDropDownCourseList(CSectionInfo.SelectedCourseID);

                //Grid must be bound after the Dropdown List is set so the value in the footer can be determined
                SectionGridView.DataBind();
            }
        }
        catch (Exception ex)
        {
            logger.ErrorException(ex.Message, ex);
            Response.Redirect("~/Error.aspx");
        }
    }

Thanks.

Was it helpful?

Solution 2

I corrected the problem...

In my markup, I set the Edit Button CausesValidation=true and ValidationGroup-MappingGrid which is what the insert validation is in the footer. To correct, I set the CausesValidation to false and removed the ValidationGroup.

I set these values in the Update Button field.

Work now!

Thanks.

OTHER TIPS

Just a guess, you are databinding the GridView on every postback.

You have a Page_Load similar to:

protected void Page_Load(object sender, EventArgs e)
{           
    populateEnrollMappingGrid(); // a method where you assign the DataSource and call grid.DataBind()
}

Use the IsPostBack property:

protected void Page_Load(object sender, EventArgs e)
{    
    if(!IsPostBack)       
        populateEnrollMappingGrid();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top