0

I recently figured out how to reference a nested RadGrid control at any time like in my answer here.

However, my main and nested grids are in edit mode and any new values in the nested grid are not there during postback. If I edit a value in any column, then click my button to fire a postback, when I access the nested RadGrid its values are unchanged (new values are the same as the old values).

aspx (not including closing tags and client side JavaScript method definitions and other random settings):

<telerik:RadGrid ID="RadGrid1" runat="server" HeaderStyle-Width="675px" Width="675px" Height="359px" PageSize="10" GridLines="None" AccessKey="0" Skin="Office2007"  ImagesPath="~/Skins/Office2007/Grid/Images" AllowFilteringByColumn="false" AllowPaging="true" AllowSorting="true" AutoGenerateColumns="false"
OnNeedDataSource="RadGrid1_NeedDataSource" OnItemDataBound="RadGrid1_ItemDataBound" OnItemCreated="RadGrid1_ItemCreated" OnEditCommand="RadGrid1_EditCommand" OnCancelCommand="RadGrid1_CancelCommand" OnUpdateCommand="RadGrid1_UpdateCommand" OnItemCommand="RadGrid1_ItemCommand" OnPreRender="RadGrid1_PreRender" >
    <MasterTableView Name="Parent" HierarchyDefaultExpanded="false" HierarchyLoadMode="ServerBind" DataKeyNames="ID" EditMode="InPlace" >
        <NestedViewTemplate>
            <asp:Panel ID="RadGrid2" runat="server" >
                <telerik:RadGrid ID="rgDetailItems" runat="server" Width="1675px" AutoGenerateColumns="false" AllowPaging="false" ShowFooter="true"
                OnEditCommand="rgDetailItems_EditCommand" OnCancelCommand="rgDetailItems_CancelCommand" OnUpdateCommand="rgDetailItems_UpdateCommand" 
                OnNeedDataSource="rgDetailItems_NeedDataSource" OnItemDataBound="rgDetailItems_ItemDataBound" >
                    <MasterTableView EditMode="InPlace">
                        <Columns>
                            <telerik:GridBoundColumn HeaderText="Amount" DataField="DtlTransAmount" UniqueName="DtlTransAmount" SortExpression="DtlTransAmount"
                            HeaderStyle-Width="20px" ItemStyle-Width="20px" FilterControlWidth="20px" DataFormatString="{0:$0.00}"/>

cs:

var items = ((RadGrid1.MasterTableView.Items[0].ChildItem as GridNestedViewItem)
    .FindControl("RadGrid2") as RadGrid).EditItems;
foreach (GridEditableItem editItem in items)
{
    Hashtable newValues = new Hashtable();
    editItem.OwnerTableView.ExtractValuesFromItem(newValues, editItem);
    foreach (DictionaryEntry de in newValues)
    {
        string valOld = (editItem.SavedOldValues[de.Key] as string) ?? "";
        string valNew = (newValues[de.Key] as string) ?? "";

        // valOld always equals valNew!
    }
}

Is there a different way to get the nested RadGrid or its items so its edited values are present?

Community
  • 1
  • 1
DanM7
  • 2,101
  • 3
  • 25
  • 45

1 Answers1

1

The cause of this issue is that the item at the 0th index is not the grid as a whole, it's the current row of the grid. The reason I don't see any new values is because I'm only ever checking the first row in the collection of rows (Items[0]).

The solution is to loop through each row in the main RadGrid, and for each row (Items[i]) get the nested RadGrid and its rows:

for (int i = 0; i < RadGrid1.EditItems.Count; i++)
{
    var items = ((RadGrid1.MasterTableView.Items[i].ChildItem as GridNestedViewItem)
        .FindControl("RadGrid2") as RadGrid).EditItems;
    foreach (GridEditableItem editItem in items)
    {
        Hashtable newValues = new Hashtable();
        editItem.OwnerTableView.ExtractValuesFromItem(newValues, editItem);
        foreach (DictionaryEntry de in newValues)
        {
            string valOld = (editItem.SavedOldValues[de.Key] as string) ?? "";
            string valNew = (newValues[de.Key] as string) ?? "";

            // valNew contains new values if they were changed by the user!
        }
    }
}
DanM7
  • 2,101
  • 3
  • 25
  • 45
  • 1
    Hi @DanM7 can you please edit your answer as I have downvoted it by mistake. Now it locked and I am not able to revert it. It allow me to upvote if you edit answer. Thanks – Bharatsing Parmar Jan 18 '17 at 12:30