编辑模式下的嵌套RadGrid具有未更改的值
本文关键字:RadGrid 模式 嵌套 编辑 | 更新日期: 2023-09-27 18:20:42
我最近发现了如何在任何时候引用嵌套的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个索引处的项不是整个网格,而是网格的当前行。我没有看到任何新值的原因是,我只检查了行集合(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!
}
}
}