编辑模式下的嵌套RadGrid值未更改

时间:2014-09-04 13:18:39

标签: c# asp.net telerik radgrid

我最近想出了如何在my answer here中随时引用嵌套的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或其项目以使其编辑的值存在?

1 个答案:

答案 0 :(得分: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!
        }
    }
}