从代码隐藏中嵌套嵌套RadGrid中的内部网格

时间:2012-05-22 17:16:42

标签: c# asp.net telerik

嗨,我正在使用radgrid上的嵌套功能。如何在OnItemDataBound事件中引用和绑定嵌套在父Radgrid中的rad网格。

我试过了

   var doc_grid = (RadGrid) item["doc_grid"].Controls[0];
   var doc_grid = (RadGrid)RadGrid1.FindControl("doc_grid");

我是否必须参考NestView ...

以下是代码的一般要点......

   <telerik:RadGrid ID="outerGrid" DataSourceID="SQL_Outer" runat="server" AutoGenerateColumns="True" OnItemDataBound="RowBound" DataKeyNames="MatNo"
        ShowGroupPanel="True">
        <MasterTableView DataSourceID="SQL_Outer" AllowMultiColumnSorting="True" DataKeyNames="MatNo" GroupLoadMode="Server">
            <Columns>
               <%--Stuff--%>
            </Columns>
            <NestedViewSettings DataSourceID="SqlDataSource2">
                <ParentTableRelation>
                    <telerik:GridRelationFields DetailKeyField="sysid" MasterKeyField="MatNo" />
                </ParentTableRelation>
            </NestedViewSettings>
            <NestedViewTemplate>

            <asp:Panel ID="NestedViewPanel" runat="server" CssClass="viewWrap">
               <div class="contactWrap">
                <telerik:RadGrid ID="doc_grid" runat="server">
                    <MasterTableView AutoGenerateColumns="False" DataKeyNames="sysid">
                      <%--Stuff--%>
                    </MasterTableView>
                </telerik:RadGrid>
                </div>
            </asp:Panel>

            </NestedViewTemplate>
        </MasterTableView>
    </telerik:RadGrid>

代码隐藏:

protected void RowBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        var item = (GridDataItem)e.Item;
        var mat_no = item.GetDataKeyValue("MatNo");

        //!!!!I Can't figure out how to Reference the Inner RadGrid!!!!
        //var doc_grid = (RadGrid) item["doc_grid"].Controls[0];
        var doc_grid = (RadGrid)RadGrid1.FindControl("doc_grid");

        using (var con = new SqlConnection())
        {
            con.ConnectionString = ConfigurationManager.ConnectionStrings["TimeMatters11ConnectionString"].ConnectionString;
            var cmd = new SqlCommand("SELECT sysid, ccode, [desc], [View] = '<a href=\"'+ filename + '\">View Doc</a>' " +
                                     "FROM lntmu11.document " +
                                     "WHERE mat_no = @matno", con);
            cmd.Parameters.AddWithValue("@matno", mat_no); 
            con.Open();
            var reader = cmd.ExecuteReader();
            doc_grid.DataSource = reader;
            doc_grid.DataBind();
            con.Close();
        }
    }  

提前致谢...

1 个答案:

答案 0 :(得分:2)

您需要转换为GridNestedViewItem而不是GridDataItem。

protected void RowBound(object sender, GridItemEventArgs e)
{
   if (e.Item is GridNestedViewItem)
   {
      GridNestedViewItem item = e.Item as GridNestedViewItem;
   }
}