Radgrid close edit mode in OnInsertCommand method

4 posts, 0 answers
  1. Jeremy
    Jeremy avatar
    36 posts
    Member since:
    Apr 2014

    Posted 22 Jun 2018 Link to this post

    After I edit/insert a record in my grid, I want the edit template to go away.  I want this to happen for two reasons.  1) Once the user edits/inserts the record, there's no need for that edit/insert row to remain.  2) The template has a required field validator on it and when the user posts back in another part of the page the required field validator gets triggers on the field in this template.


    I've tried MyGroupsRadGrid.MasterTableView.ClearEditItems() after the insert logic on MyGroupsRadGrid_InsertCommand() but that doesn't work.  I've tried e.Canceled = true but that doesn't work.  I've tried e.Item.Edit = false but that errors.

    <telerik:RadGrid ID="MyGroupsRadGrid" runat="server" OnNeedDataSource="MyGroupsRadGrid_NeedDataSource"
        AutoGenerateColumns="false" AutoPostBackOnFilter="true" OnUpdateCommand="MyGroupsRadGrid_UpdateCommand" OnItemDataBound="MyGroupsRadGrid_ItemDataBound"
        AllowSorting="true" AllowAutomaticUpdates="True" AllowAutomaticInserts="True" OnEditCommand="MyGroupsRadGrid_EditCommand" OnSelectedIndexChanged="MyGroupsRadGrid_SelectedIndexChanged1"
        AllowFilteringByColumn="false" OnCancelCommand="MyGroupsRadGrid_CancelCommand" MasterTableView-CommandItemDisplay="Top" OnInsertCommand="MyGroupsRadGrid_InsertCommand">
        <ClientSettings AllowKeyboardNavigation="true" EnablePostBackOnRowClick="true" EnableRowHoverStyle="true">
            <Selecting AllowRowSelect="True" />
        <MasterTableView DataKeyNames="BWGroupID">
            <EditFormSettings EditFormType="Template">
                    <table id="Table1" class="marginleft50 bordercollapse width100percent" cellspacing="2" cellpadding="1" border="0" rules="none">
                            <td valign="top">
                                <table id="Table2" class="bordercollapse" cellspacing="2" cellpadding="1" border="0" rules="none">
                                    <tr id="BWGroupIDRow" runat="server">
                                        <td>Group ID   </td>
                                        <td><asp:Label ID="BWGroupIDLabel" runat="server" Text='<%# Bind("BWGroupID") %>' /></td>
                                    <tr id="BWGroupNameRow" runat="server">
                                        <td>Group Name   </td>
                                            <asp:TextBox ID="GroupNameTextBox" runat="server" Text='' />
                                            <asp:RequiredFieldValidator ID="RequiredGroupName" runat="server" ControlToValidate="GroupNameTextBox" ForeColor="Red" Text="*A Group Name is Required." />
                                    <tr id="DisplayNameRow" runat="server">
                                        <td>Group Owner   </td>
                                            <telerik:RadComboBox ID="OwnerRadComboBox" runat="server" OnClientSelectedIndexChanged="enableAddSelectionButton" Width="270px" DataTextField="Value" DataValueField="Key" ValidationExpression="[\w\s\.,_-]+$" AppendDataBoundItems="True" Filter="Contains">
                                                    <telerik:RadComboBoxItem runat="server" Text="" Value="-1" />
                                            <br />
                                            <asp:Label ID="OwnerError" runat="server" ForeColor="Red" Visible="true" />
                                    <tr id="IsActiveRow" runat="server">
                                        <td>Status   </td>
                                        <td><asp:Label ID="Label8" runat="server" Text='<%# Eval("IsActive").ToString() == "True" ? "Active" : "Inactive" %>' /></td>
                                        <td><asp:CheckBox ID="DeactivateCheckBox" runat="server" Text="Deactivate Group" /></td>
                                        <td align="center" colspan="2">
                                            <br />
                                            <asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
                                                runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>' /> 
                                            <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" CommandName="Cancel" />
                            <td valign="top"></td>
                    <br />
                <telerik:GridEditCommandColumn ButtonType="LinkButton" EditText="Edit" UniqueName="EditCommandColumn"></telerik:GridEditCommandColumn>
                <telerik:GridBoundColumn UniqueName="BWGroupID" DataField="BWGroupID" HeaderText="Group ID" SortExpression="BWGroupID" FilterControlWidth="150px" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" />
                <telerik:GridBoundColumn DataField="BWGroupName" HeaderText="Group Name" ReadOnly="true" SortExpression="BWGroupName" FilterControlWidth="150px" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" />
                <telerik:GridBoundColumn DataField="ComputedBWGroupUserCount" HeaderText="Member Count" SortExpression="ComputedBWGroupUserCount" FilterControlWidth="50px" AutoPostBackOnFilter="true" />

    Code behind:

    protected void MyGroupsRadGrid_InsertCommand(object sender, GridCommandEventArgs e)
        int parseOwnerId = -1;
            GridEditableItem editedItem = (GridEditableItem)e.Item;
            if (e.CommandName == "PerformInsert")
                #region Get controls
                TextBox GroupNameTextBox = new TextBox();
                GroupNameTextBox = (TextBox)e.Item.FindControl("GroupNameTextBox");
                RadComboBox OwnerRadComboBox = (RadComboBox)editedItem.FindControl("OwnerRadComboBox");
                int.TryParse(OwnerRadComboBox.SelectedValue, out parseOwnerId);
                BWGroup newGroup = new BWGroup();
                newGroup.BWUserIDCreated = BWSessionHandler.BWID;
                BWUser selectedOwner = new BWUser();
                if (parseOwnerId > 0)
                    selectedOwner = this._bwContext.BWUsers.Where(x => x.BWUserID == parseOwnerId).FirstOrDefault();
                    if (selectedOwner.BWUserID != null && selectedOwner.BWUserID > 0)
                        newGroup.BWUserIDOwner = selectedOwner.BWUserID;
                newGroup.BWUserIDUpdated = BWSessionHandler.BWID;
                newGroup.BWGroupName = GroupNameTextBox.Text;
                newGroup.IsActive = true;
                newGroup.SystemGenerated = false;
                newGroup.RowInsertDateTime = DateTime.Now;
                ViewState["SelectedGroupId"] = newGroup.BWGroupID;
                MyGroupsRadGrid.MasterTableView.Items[0].Edit = false;
                GroupMemberRadGrid.MasterTableView.IsItemInserted = false;
                GroupMemberRadGrid.Enabled = true;
                foreach (GridDataItem row in MyGroupsRadGrid.MasterTableView.Items)
                    if (row.GetDataKeyValue("BWGroupID").ToString() == ViewState["SelectedGroupId"].ToString())
                        row.Selected = true;
                e.Canceled = true;
        catch (Exception ex) { this.ThrowError(ex, parseOwnerId); }
  2. Eyup
    Eyup avatar
    4046 posts

    Posted 26 Jun 2018 Link to this post

    Hello Jeremy,

    Since you are manually updating the database using the grid events, there is no need to have the AllowAutomaticInserts. Please remove this property and the issue should be resolved.

    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  3. Jeremy
    Jeremy avatar
    36 posts
    Member since:
    Apr 2014

    Posted 27 Jun 2018 in reply to Eyup Link to this post

    Hi Eyup,

    Thanks for the reply.  I removed all references to "AllowAutomatic" in the grids (Inserts, Updates and Deletes) because you're right, we're doing it manually.  But the edit/insert row still doesn't go away.  I haven't changed any of the other code so it still looks as it does I my OP.  Any other ideas why it's not working?  Again, I've tried a few ways of closing the edit row.  What's the proper way of doing this?

  4. Jeremy
    Jeremy avatar
    36 posts
    Member since:
    Apr 2014

    Posted 27 Jun 2018 in reply to Eyup Link to this post

    Ok, I think I have it.  I was using the right code on the wrong grid.  It looks like

    .MasterTableView.IsItemInserted = false;

    is the right code but I was using it on the Group Members grid when I should have been using it on the Groups grid.  Once I applied it to MyGroupsRadGrid, it worked!

    Thank you!

Back to Top