2014-09-04 69 views
0

我最近弄清楚如何在my answer here的任何時候引用一個嵌套的RadGrid控件。編輯模式下的嵌套RadGrid值沒有變化

但是,我的主網格和嵌套網格處於編輯模式,嵌套網格中的任何新值在回發期間都不存在。如果我在任何列中編輯一個值,然後點擊我的按鈕來觸發回發,當我訪問嵌套的RadGrid時,其值保持不變(新值與舊值相同)。

ASPX(不包括結束標籤和客戶端的JavaScript方法定義和其他隨機設置):

<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! 
    } 
} 

是否有不同的方式來獲得嵌套radgrid控件或它的項目,所以它的編輯值存在?

+0

能否請您提供您的aspx頁面的代碼? – 2014-09-04 16:20:33

+0

發佈爲網格的aspx標記。我刪除了'ClientSettings'和'EditItemStyle'等額外信息。 – DanM7 2014-09-04 16:33:43

+0

我得到了答案@JayeshGoyani。感謝您看看這個。 – DanM7 2014-09-05 21:03:11

回答

1

此問題的原因在於第0個索引處的項目不是網格整體,而是網格的當前行。我沒有看到任何新值的原因是因爲我只檢查行集合中的第一行(Items[0])。

的解決方案是通過循環在主radgrid控件的每一行,並且每行(Items[i])獲得嵌套radgrid控件及其行:

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! 
     } 
    } 
} 
+1

您好@ DanM7您可以請編輯您的答案,因爲我錯誤地低估了它。現在它鎖定了,我無法恢復它。如果你編輯答案,它可以讓我喜歡。謝謝 – 2017-01-18 12:30:35

+1

當然,只是編輯。我欣賞upvote! – DanM7 2017-01-19 03:15:05