aspxgridview从父级获取MasterRowKeyValue

时间:2010-09-14 19:02:36

标签: devexpress aspxgridview

我正在尝试在网格中嵌套网格 要填充最内部网格,我需要来自网格直接父级和网格父级父级的KeyValue。
如何从大多数父母那里检索MasterRowKeyValue? 我有3个网格嵌套如下:

<dxwgv:ASPxGridView ID="gridParents" runat="server" ObjectDataSource="odsParents" KeyFieldName="ParentId">
 <SettingsDetail ShowDetailRow="true" />
 <Columns>
  <dx:GridViewDataColumn FieldName="ParentIdId" Visible="false" />
  <dx:GridViewDataColumn FieldName="Title" />
 </Columns>
 <Templates>
  <DetailRow>
   <dxwgv:ASPxGridView ID="gridParentsChilren" runat="server" ObjectDataSource="odsParentsChildren" KeyFieldName="ChildId" OnBeforePerformDataSelect="gridParentsChilren_DataSelect">
    <SettingsDetail ShowDetailRow="true" IsDetailGrid="true" />
    <Columns>
     <dx:GridViewDataColumn FieldName="ChildId" Visible="false" />
     <dx:GridViewDataColumn FieldName="Title" />
    </Columns>
    <Templates>
     <dxwgv:ASPxGridView ID="gridParentsChilrenRoles" runat="server" ObjectDataSource="odsParentsChildrenRoles" KeyFieldName="RoleId" OnBeforePerformDataSelect="gridParentsChilrenRoles_DataSelect">
      <SettingsDetail ShowDetailRow="true" IsDetailGrid="true" />
      <Columns>
       <dx:GridViewDataColumn FieldName="RoleId" Visible="false" />
       <dx:GridViewDataColumn FieldName="Title" />
      </Columns>
     </dxwgv:ASPxGridView>
    </Templates>
   </dxwgv:ASPxGridView>
  </DetailRow>
 </Templates>
</dxwgv:ASPxGridView>
<asp:ObjectDataSource id="odsParents" runat="server" SelectMethod="GetParents" />
<asp:ObjectDataSource id="odsParentsChildren" runat="server" SelectMethod="GetParentsChildren">
 <SelectParameters>
  <asp:Parameter Name="parentid" Type="Object" />
 </SelectParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource id="odsParentsChildrenRoles" runat="server" SelectMethod="GetParentsRoles">
 <SelectParameters>
  <asp:Parameter Name="childid" Type="Object" />
  <asp:Parameter Name="parentid" Type="Object" />
 </SelectParameters>
</asp:ObjectDataSource>

当为第一个Detail网格调用OnBeforePerformDataSelect时,我设置了选择参数DefaultValue,如下所示:

odsParentsChildren.SelectParameters["parentid"].DefaultValue = ((ASPxGridView)sender).GetMasterRowKeyValue().ToString();

对于最内部(第二个)嵌套网格,我需要将我的一个参数设置为最外网格的MasterRowKeyValue。
类似的东西:

odsParentsChildrenRoles.SelectParameters["childid"].DefaultValue = ((ASPxGridView)sender).GetMasterRowKeyValue().ToString();
odsParentsChildrenRoles.SelectParameters["parentid"].DefaultValue = ((ASPxGridView)sender).Master.GetMasterRowKeyValue().ToString();

我如何实现这一目标?

2 个答案:

答案 0 :(得分:2)

如果为已处理的详细信息确定主ASPxGridView实例,则可以执行此操作。这可以使用以下代码完成:

protected void ASPxGridView1_BeforePerformDataSelect(object sender, EventArgs e) {
        ASPxGridView subDetail = sender as ASPxGridView;
        GridViewDetailRowTemplateContainer container = subDetail.NamingContainer as GridViewDetailRowTemplateContainer;
        ASPxGridView detail = container.Grid;
        odsParentsChildrenRoles.SelectParameters["parentid"].DefaultValue = detail.GetMasterRowKeyValue();
    }

答案 1 :(得分:0)

另一个可能的版本

protected void gvMyGridView_BeforePerformDataSelect(object sender, EventArgs e)
    {
        var gvMyGridView = sender as ASPxGridView;
        var parentRow = gvMyGridView.NamingContainer as GridViewDetailRowTemplateContainer;
        odsChildDataSource.SelectParameters["Id"].DefaultValue = parentRow .KeyValue.ToString();
    }