This is a migrated thread and some comments may be shown as answers.

Merge Programmatic binding + Hierarchical data-binding + n levels

1 Answer 107 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Leonardo
Top achievements
Rank 1
Leonardo asked on 15 Sep 2010, 06:43 PM
Hi,
I need to build a dynamic grid with n-levels hierarchy. Also I need to define n-columns depending of the table the user expanded. To do this, I have 3 object-datasource an a placeholder to programmatically define the radgrid and the detailsgrids asociated. The problem is that when I want to access to the 2nd level (the 1st one is ok), the #grdEntity_DetailTableDataBind event dont fire.

aspx
       
<telerik:RadScriptManager ID="RadScriptManager1" runat="server" />
<!-- Only for master table - level 0 -->
        <asp:ObjectDataSource ID="odsCustomerEntities" runat="server" SelectMethod="GetCustomerEntitiesRows"
            TypeName="EKSClientLibrary.Ecl.clsEclKernelClient" EnablePaging="True" MaximumRowsParameterName="RowCount"
            StartRowIndexParameterName="StartingRow">
            <SelectParameters>
                <asp:Parameter Name="StartingRow" Type="Int32" DefaultValue="1" />
                <asp:Parameter Name="RowCount" Type="Int32" DefaultValue="25" />
                <asp:Parameter Name="pCustPkey" Type="String" DefaultValue="" />
            </SelectParameters>
        </asp:ObjectDataSource>
        <!-- For header tables -> Level 1 - 3 - n -->
        <asp:ObjectDataSource ID="odsEntities" runat="server" SelectMethod="GetEntitiesRows"
            TypeName="EKSClientLibrary.Ecl.clsEclKernelClient" EnablePaging="True" MaximumRowsParameterName="RowCount"
            StartRowIndexParameterName="StartingRow">
            <SelectParameters>
                <asp:Parameter Name="StartingRow" Type="Int32" DefaultValue="1" />
                <asp:Parameter Name="RowCount" Type="Int32" DefaultValue="25" />
                <asp:Parameter Name="pParentPkey" Type="String" DefaultValue="" />
            </SelectParameters>
        </asp:ObjectDataSource>
   <!-- For detail tables -> Level 2 - 4 - n -->
<asp:ObjectDataSource ID="odsEntitiesDetails" runat="server" SelectMethod="GetEntitiesDetailsRows"
            TypeName="EKSClientLibrary.Ecl.clsEclKernelClient" EnablePaging="True" MaximumRowsParameterName="RowCount"
            StartRowIndexParameterName="StartingRow">
            <SelectParameters>
                <asp:Parameter Name="StartingRow" Type="Int32" DefaultValue="1" />
                <asp:Parameter Name="RowCount" Type="Int32" DefaultValue="25" />
                <asp:Parameter Name="pPkey" Type="String" DefaultValue="" />
                <asp:Parameter Name="pParentPkey" Type="String" DefaultValue="" />
                <asp:Parameter Name="pRelatedTable" Type="String" DefaultValue="" />
            </SelectParameters>
        </asp:ObjectDataSource>
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="PlaceHolder1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="PlaceHolder1" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>


vb

   
Private Sub DefineGridStructure()
        'Definicion global
        grdEntity = New RadGrid
        Dim column1 As GridBoundColumn
 
        'Nivel 0
        With grdEntity
            .DataSourceID = "odsCustomerEntities"
            .ID = "grdEntity"
            .Width = Unit.Percentage(100)
            .PageSize = 5
            .AllowPaging = True
            .PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric
 
            'Columnas
            .AutoGenerateColumns = False
            column1 = New GridBoundColumn
            column1.DataField = "C03"
            column1.HeaderText = "Entidad"
            .MasterTableView.Columns.Add(column1)
 
            .GroupingEnabled = False
            .ShowGroupPanel = False
            .ShowStatusBar = True
            .ClientSettings.AllowDragToGroup = False
 
            .MasterTableView.PageSize = 15
            .MasterTableView.DataKeyNames = New String(2) {"Pkey", "ParentPkey", "RelatedTable"}
            .MasterTableView.EnableViewState = True
            '.MasterTableView.HierarchyLoadMode = GridChildLoadMode.ServerOnDemand
 
        End With
 
        'Nivel 1
        Dim view1 = New GridTableView(grdEntity)
        view1.Name = "EntityDetails"
        view1.EnableViewState = True
        grdEntity.MasterTableView.DetailTables.Add(view1)
 
        'Add grid to place holder
        Me.PlaceHolder1.Controls.Add(grdEntity)
    End Sub
 
    Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        mCustomerPkey = ""
        DefineGridStructure()
    End Sub
 
    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        mCustomerPkey = Request.QueryString("CustPkey")
        Dim grid As RadGrid = CType(PlaceHolder1.FindControl("grdEntity"), RadGrid)
        RadAjaxManager1.AjaxSettings.AddAjaxSetting(grid, grid)
    End Sub
 
    Public Sub odsCustomerEntities_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs) Handles odsCustomerEntities.Selecting
        With e.InputParameters
            .Item("pCustPkey") = mCustomerPkey
        End With
    End Sub
 
    Private Sub grdEntity_DetailTableDataBind(ByVal source As Object, ByVal e As Telerik.Web.UI.GridDetailTableDataBindEventArgs) Handles grdEntity.DetailTableDataBind
        Debug.Print("grdEntity_DetailTableDataBind")
        Dim dataItem As GridDataItem = CType(e.DetailTableView.ParentItem, GridDataItem)
        Select Case e.DetailTableView.Name
            Case "EntityDetails"
                Dim relationFields As GridRelationFields
                Dim wPkey As String = dataItem.GetDataKeyValue("Pkey").ToString()
                Dim wParentPkey As String = dataItem.GetDataKeyValue("ParentPkey").ToString()
                Dim wRelatedTable As String = dataItem.GetDataKeyValue("RelatedTable").ToString()
                With e.DetailTableView
                    .DataSourceID = "odsEntitiesDetails"
                    .DataKeyNames = New String(2) {"Pkey", "ParentPkey", "RelatedTable"}
                    .Width = Unit.Percentage(100)
 
                    'Columnas
                    '.AutoGenerateColumns = True
                    Dim wColumn As GridBoundColumn
                    wColumn = New GridBoundColumn
                    wColumn.DataField = "Pkey"
                    wColumn.HeaderText = "Pkey"
                    .Columns.Add(wColumn)
                    wColumn = New GridBoundColumn
                    wColumn.DataField = "ParentPkey"
                    wColumn.HeaderText = "ParentPkey"
                    .Columns.Add(wColumn)
                    wColumn = New GridBoundColumn
                    wColumn.DataField = "RelatedTable"
                    wColumn.HeaderText = "RelatedTable"
                    .Columns.Add(wColumn)
                    Dim wEntityColumns As EntityColumnType()
                    wEntityColumns = GetEksClientInstance.GetEntitiesDetailsColumns(GetTicket(), wPkey, wRelatedTable)
                    For i = 0 To wEntityColumns.GetLength(0) - 1
                        wColumn = New GridBoundColumn
                        'wColumn.DataField = wEntityColumns(i).Code
                        wColumn.DataField = "C0" & i.ToString.Trim
                        wColumn.HeaderText = wEntityColumns(i).Description
                        .Columns.Add(wColumn)
                    Next
 
                    'Relaciones
                    relationFields = New GridRelationFields()
                    relationFields.MasterKeyField = "Pkey"
                    relationFields.DetailKeyField = "pPkey"
                    .ParentTableRelation.Add(relationFields)
 
                    relationFields = New GridRelationFields()
                    relationFields.MasterKeyField = "ParentPkey"
                    relationFields.DetailKeyField = "pParentPkey"
                    .ParentTableRelation.Add(relationFields)
 
                    relationFields = New GridRelationFields()
                    relationFields.MasterKeyField = "RelatedTable"
                    relationFields.DetailKeyField = "pRelatedTable"
                    .ParentTableRelation.Add(relationFields)
 
                    'Nivel 2
                    Dim view2 = New GridTableView(grdEntity)
                    view2.Name = "Entity"
                    view2.EnableViewState = True
                    .DetailTables.Add(view2)
                End With
            Case "Entity"
                Dim relationFields As GridRelationFields
                Dim wPkey As String = dataItem.GetDataKeyValue("Pkey").ToString()
                Dim wParentPkey As String = dataItem.GetDataKeyValue("ParentPkey").ToString()
                Dim wRelatedTable As String = dataItem.GetDataKeyValue("RelatedTable").ToString()
                With e.DetailTableView
                    .DataSourceID = "odsEntities"
                    .Width = Unit.Percentage(100)
                    .DataKeyNames = New String(2) {"Pkey", "ParentPkey", "RelatedTable"}
 
                    'Columnas
                    Dim column1 = New GridBoundColumn
                    column1.DataField = "C03"
                    column1.HeaderText = "Entidad"
                    .Columns.Add(column1)
 
                    'Relaciones
                    relationFields = New GridRelationFields()
                    relationFields.MasterKeyField = "Pkey"
                    relationFields.DetailKeyField = "pPkey"
                    .ParentTableRelation.Add(relationFields)
 
                    relationFields = New GridRelationFields()
                    relationFields.MasterKeyField = "ParentPkey"
                    relationFields.DetailKeyField = "pParentPkey"
                    .ParentTableRelation.Add(relationFields)
 
                    relationFields = New GridRelationFields()
                    relationFields.MasterKeyField = "RelatedTable"
                    relationFields.DetailKeyField = "pRelatedTable"
                    .ParentTableRelation.Add(relationFields)
 
                    'Nivel 1
                    Dim view1 = New GridTableView(grdEntity)
                    view1.Name = "EntityDetails"
                    view1.EnableViewState = True
                    .DetailTables.Add(view1)
                End With
        End Select
    End Sub

1 Answer, 1 is accepted

Sort by
0
Radoslav
Telerik team
answered on 21 Sep 2010, 10:12 AM
Hi Leonardo,

To create the RadGrid programmatically you need to define its structure into the Page_Init event handler. Defining the detail tables columns into the DetailTableDataBind event is not supported. Please check out the following online example which demonstrates how to archive the desired functionality:
http://demos.telerik.com/aspnet-ajax/grid/examples/programming/hierarchy/defaultcs.aspx

Additionally you could also check out the following online resources which explains the Hierarchical data-binding using declarative relations, Hierarchical data-binding using DetailTableDataBind event and autogenerate hierarchy.

I hope this helps.

Sincerely yours,
Radoslav
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Leonardo
Top achievements
Rank 1
Answers by
Radoslav
Telerik team
Share this question
or