This is a migrated thread and some comments may be shown as answers.

Radgrid close edit mode in OnInsertCommand method

3 Answers 613 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jeremy
Top achievements
Rank 1
Jeremy asked on 22 Jun 2018, 11:54 PM

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" />
    </ClientSettings>
    <MasterTableView DataKeyNames="BWGroupID">
        <EditFormSettings EditFormType="Template">
            <FormTemplate>
                <table id="Table1" class="marginleft50 bordercollapse width100percent" cellspacing="2" cellpadding="1" border="0" rules="none">
                    <tr>
                        <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>
                                <tr id="BWGroupNameRow" runat="server">
                                    <td>Group Name   </td>
                                    <td>
                                        <asp:TextBox ID="GroupNameTextBox" runat="server" Text='' />
                                        <asp:RequiredFieldValidator ID="RequiredGroupName" runat="server" ControlToValidate="GroupNameTextBox" ForeColor="Red" Text="*A Group Name is Required." />
                                    </td>
                                </tr>
                                <tr id="DisplayNameRow" runat="server">
                                    <td>Group Owner   </td>
                                    <td>
                                        <telerik:RadComboBox ID="OwnerRadComboBox" runat="server" OnClientSelectedIndexChanged="enableAddSelectionButton" Width="270px" DataTextField="Value" DataValueField="Key" ValidationExpression="[\w\s\.,_-]+$" AppendDataBoundItems="True" Filter="Contains">
                                            <Items>
                                                <telerik:RadComboBoxItem runat="server" Text="" Value="-1" />
                                            </Items>
                                        </telerik:RadComboBox>
                                        <br />
                                        <asp:Label ID="OwnerError" runat="server" ForeColor="Red" Visible="true" />
                                    </td>
                                </tr>
                                <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>
                                </tr>
                                <tr>
                                    <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>
                                </tr>
                            </table>
                        </td>
                        <td valign="top"></td>
                    </tr>
                </table>
                <br />
            </FormTemplate>
        </EditFormSettings>
        <Columns>
            <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" />
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

Code behind:

protected void MyGroupsRadGrid_InsertCommand(object sender, GridCommandEventArgs e)
{
    int parseOwnerId = -1;
 
    try
    {
        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);
            #endregion
 
            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;
 
            this._bwContext.BWGroups.InsertOnSubmit(newGroup);
            this._bwContext.SubmitChanges();
 
            ViewState["SelectedGroupId"] = newGroup.BWGroupID;
            MyGroupsRadGrid.MasterTableView.Items[0].Edit = false;
            MyGroupsRadGrid.MasterTableView.ClearEditItems();
            MyGroupsRadGrid.Rebind();
 
            GroupMemberRadGrid.MasterTableView.IsItemInserted = false;
            GroupMemberRadGrid.Rebind();
            AssignNameOfGroup(newGroup.BWGroupName);
            GroupMemberRadGrid.Enabled = true;
 
            foreach (GridDataItem row in MyGroupsRadGrid.MasterTableView.Items)
            {
                if (row.GetDataKeyValue("BWGroupID").ToString() == ViewState["SelectedGroupId"].ToString())
                {
                    row.Selected = true;
                    break;
                }
            }
            e.Canceled = true;
        }
    }
    catch (Exception ex) { this.ThrowError(ex, parseOwnerId); }
}

3 Answers, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 27 Jun 2018, 04:44 AM
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.

Regards,
Eyup
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.
0
Jeremy
Top achievements
Rank 1
answered on 27 Jun 2018, 03:43 PM

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?

0
Jeremy
Top achievements
Rank 1
answered on 27 Jun 2018, 03:53 PM

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!

Tags
Grid
Asked by
Jeremy
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Jeremy
Top achievements
Rank 1
Share this question
or