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

RadGrid Batch Edit/Update Issue

1 Answer 245 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 17 Nov 2014, 04:09 PM
Hello,

I am trialing the radgrid controls and am having an issue with the batch update mode.

I have the following html in aspx:
<telerik:RadGrid ID="RadGrid_MobileCheckList" runat="server"
                AutoGenerateColumns="False" GridLines="Vertical" AllowMultiRowEdit="true"
                HeaderStyle-CssClass="radGridHeaderStyle"
                onitemdatabound="RadGrid_MobileCheckList_ItemDataBound"
                onprerender="RadGrid_MobileCheckList_PreRender"
                onitemcommand="RadGrid_MobileCheckList_ItemCommand">
                <MasterTableView DataKeyNames="MobileChecklist_ID, MobileResponseType_ID, MobileResponse_ID" EditMode="Batch" CommandItemDisplay="Bottom">
                    <Columns>
                        <telerik:GridBoundColumn AllowSorting="true" HeaderStyle-Width="20%" ItemStyle-CssClass="radGridItemStyle" ReadOnly="true" DataField="Task" HeaderText="Task"></telerik:GridBoundColumn>
                        <telerik:GridTemplateColumn HeaderStyle-Width="10%" ItemStyle-CssClass="radGridItemStyle" HeaderText="Response" UniqueName="">
                            <EditItemTemplate>
                                <telerik:RadComboBox runat="server" ID="MobileChecklistGrid_RadComboBox_Response" ondatabound="MobileChecklistGrid_RadComboBox_Response_DataBound" Width="100%" DataTextField="Response" DataValueField="MobileResponse_ID"></telerik:RadComboBox>
                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn HeaderStyle-Width="70%" ItemStyle-CssClass="radGridItemStyle" HeaderText="Comment">
                            <EditItemTemplate>
                                <telerik:RadTextBox runat="server" ID="MobileChecklistGrid_TextBox_Comment" Width="100%" Text='<%# Eval("Comment") %>'></telerik:RadTextBox>
                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridEditCommandColumn UniqueName="EditCommandColumn" />
                    </Columns>
                    <CommandItemTemplate>
                        <asp:Button runat="server" ID="UpdateAll" Text="Update All" CommandName="UpdateAll" />
                    </CommandItemTemplate>
                </MasterTableView>
            </telerik:RadGrid>


This grid is populated on a selected row event from another grid:
protected void RadGrid_CheckList_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "RowClick")
        {
            GridDataItem item = (GridDataItem)e.Item;
        string checklistId = item["Checklist_ID"].Text;
            DataTable table = _domain.GetMobileCheckListDetailsByCheckListId(checklistId);
 
            RadGrid_MobileCheckList.DataSource = table;
            RadGrid_MobileCheckList.DataBind();
        }
    }


Also, I am populating the combobox that is embedded in the radgrid on the ItemDataBound Event:
protected void RadGrid_MobileCheckList_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                GridEditableItem item = (GridEditableItem)e.Item;
                RadComboBox rcb = item.FindControl("MobileChecklistGrid_RadComboBox_Response") as RadComboBox;
 
                if (rcb != null)
                {
                    int? responseTypeId = !string.IsNullOrEmpty(item.GetDataKeyValue("MobileResponseType_ID").ToString()) ? int.Parse(item.GetDataKeyValue("MobileResponseType_ID").ToString()) : (int?)null;
                    if (responseTypeId != null)
                        {
                           DataTable table =  _domain.GetMobileResponsesByResponseTypeID(responseTypeId);
                           comboBox.DataSource = table;
                           comboBox.DataValueField = table.Columns[valueField].ToString();
                           comboBox.DataTextField = table.Columns[textField].ToString();
                           comboBox.DataBind();
                           comboBox.SelectedIndex = -1;
                        }
                }
 
            }
        }
    }


Finally, I am setting each row as editable in the PreRender event:
protected void RadGrid_MobileCheckList_PreRender(object sender, EventArgs e)
   {
       foreach (GridDataItem dataItem in RadGrid_MobileCheckList.Items)
       {
           dataItem.Edit = true;
       }
       RadGrid_MobileCheckList.Rebind();
   }


When the "Update All" button is clicked I am iterating through each row to determine if there are updates in the ItemCommand Event:
I have tried many differnt things here but the results from the ExtractValuesFromItem is always Count=0.

protected void RadGrid_MobileCheckList_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == "UpdateAll")
        {
            foreach (GridEditableItem editedItem in RadGrid_MobileCheckList.EditItems)
            {
                Hashtable newValues = new Hashtable();
                Hashtable oldValues = new Hashtable();
                oldValues = (Hashtable)editedItem.SavedOldValues;
                e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
                editedItem.ExtractValues(newValues);
 
               //I can get the items with something like this:
               //But I dont want to update every row everytime, I need to know which items have been actually edited. Something like an isDirty property.
                 var test = ((RadComboBox)editedItem.Cells[3].Controls[1]).SelectedItem;
                 var test2 = ((RadTextBox)editedItem.Cells[4].Controls[1]).Text;
            }
        }
    }

How can I determine if the row has actually changed? I don't want to update every row, only the items that have actually been changed.

Thanks,






















‚Äč

1 Answer, 1 is accepted

Sort by
0
Angel Petrov
Telerik team
answered on 20 Nov 2014, 01:56 PM
Hello,

Actually the approach illustrated is not exactly correct. It is important to know that when the EditMode is set to Batch RadGrid will render only one editor for the entire column. Meaning that only one MobileChecklistGrid_RadComboBox_Response combo box will be created. Moreover the OnItemDataBound event will not be fired for the hidden panel in which the editors are placed and you will not be able to populate them inside the RadGrid_MobileCheckList_ItemDataBound handler. Note however that you can bind them by subscribing to the PreRender event and obtaining a reference to them using the GetBatchColumnEditor or GetBatchEditorContainer method as shown here.

After you modify the code logic according to the above you should be able to edit the records on the client and later perform a single request to update them. Bear in mind that when following this scenario you can only edit one record at a time(multi-row editing is not supported as the grid renders one editor) meaning that it would be best to comment the RadGrid_MobileCheckList_PreRender handler logic.

Also you should alter the save button definition so that the Batch editing manager saveChanges method is called(example provided below).

ASPX:
<CommandItemTemplate>
                       <asp:Button runat="server" ID="UpdateAll" Text="Update All" OnClientClick="saveChanges(); return false;" />
                   </CommandItemTemplate>

JavaScript:
function saveChanges() {
               var grid = $telerik.findControl(document, "RadGrid_MobileCheckList");
               grid.get_batchEditingManager().saveChanges(grid.get_masterTableView);
           }
With the above code you will force the RadGrid to fire a command and send the user changes on the server. The last can later be accessed by subscribing to the OnBatchEditComnand event as demonstrated in the above linked article.

Regards,
Angel Petrov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Kevin
Top achievements
Rank 1
Answers by
Angel Petrov
Telerik team
Share this question
or