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

Issues with GridTableView

1 Answer 24 Views
Grid
This is a migrated thread and some comments may be shown as answers.
smith spd
Top achievements
Rank 1
smith spd asked on 10 Aug 2010, 07:17 AM
Hello Telerik Team,

I am trying to bind the xml to the Grid but having issues rendering the child nodes from the xml properly.
Please have a look at my xml and code snippet and help me in resolving the issue.

I have referred this blog to achieve this functionality

<?xml version="1.0" encoding="utf-8"?>
<root>
  <Products>
    <Product>
      <Items ItemID="1" ItemNo="1232">
        <Description ManufactureDate="2010/05/26" ID="11"></Description>
      </Items>
      <Items ItemID="2" ItemNo="1231">
        <Description ManufactureDate="2010/05/12" ID="12"></Description>
      </Items>
      <Items ItemID="3" ItemNo="1270">
        <Description ManufactureDate="2010/05/1" ID="21"></Description>
      </Items>
      <Items ItemID="4" ItemNo="1129">
        <Description ManufactureDate="2010/05/4" ID="30"></Description>
      </Items>
    </Product>
  </Products>
</root>

Below is my code snippet:

Imports Telerik.Web.UI
Imports System.Xml
Imports System.IO
 
Public Class Test
    Inherits System.Web.UI.Page
    Dim xmlDoc As New System.Xml.XmlDocument()
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        DefineGridStructure()
 
        Dim Grid1 As RadGrid = CType(PlaceHolder1.FindControl("RadGrid1"), RadGrid)
        RadAjaxManager1.AjaxSettings.AddAjaxSetting(Grid1, Grid1)
 
        If Not IsPostBack Then
            Grid1.SelectedIndexes.Add(1, 0, 0)
            Grid1.DataBind()
            '  Grid1.MasterTableView.Items(0).Expanded = True
        Else
 
        End If
         
        Grid1.DataBind()
    End Sub
 
    Private Sub DefineGridStructure()
        Dim RadGrid1 As RadGrid = New RadGrid
        RadGrid1.ID = "RadGrid1"
 
 
        xmlDoc.Load(Server.MapPath("test.xml"))
 
        xmlDoc.SelectSingleNode("//Product").Attributes.RemoveAll()
 
        Dim ndlSingleParentNode As XmlNodeList = xmlDoc.SelectNodes("//Product/node() [descendant::node()][position()=1]")
 
 
        Dim ndl As XmlNodeList = xmlDoc.SelectNodes("//Product/node()[descendant::node()]")
 
        RadGrid1.DataSource = ConvertXmlNodeListToDataTable(ndlSingleParentNode, ndl, RadGrid1)
        RadGrid1.DataBind()
        Me.PlaceHolder1.Controls.Add(RadGrid1)
        ' Dim ndList As System.Xml.XmlNodeList = xmlDoc.SelectNodes("//Product/node()")
    End Sub
 
    Private Function ConvertXmlNodeListToDataTable(ByVal ndlProduct As XmlNodeList, ByVal ndl As XmlNodeList, ByVal RadGrid1 As RadGrid) As Object
 
        Dim dt As DataTable = New DataTable()
 
        Dim TempColumn As Integer = 0
        For Each ndSubChild As XmlNode In ndlProduct
            For attrCoun As Integer = 0 To ndSubChild.Attributes.Count - 1 Step 1
                TempColumn = TempColumn + 1
                Dim dc As DataColumn = New DataColumn(ndSubChild.Attributes(attrCoun).Name, System.Type.GetType("System.String"))
                If dt.Columns.Contains(ndSubChild.Attributes(attrCoun).Name) Then
                    dt.Columns.Add(dc.ColumnName = dc.ColumnName + TempColumn.ToString())
                Else
                    dt.Columns.Add(dc)
                End If
            Next
        Next
        Dim ColumnsCount As Integer = dt.Columns.Count
 
        Dim ndCount As Integer = 0
        For ndlCount As Integer = 0 To ndl.Count - 1 Step 1
            Dim dr As DataRow = dt.NewRow()
            For ndCount = 0 To ColumnsCount - 1 Step 1
                If Not ndl(ndlCount).Attributes(ndCount).Value = "" Then
                    dr(ndCount) = ndl(ndlCount).Attributes(ndCount).Value
                Else
                    dr(ndCount) = ndl(ndlCount).Attributes(ndCount).Value
                End If
            Next
            dt.Rows.Add(dr)
        Next
 
        Dim tableView As GridTableView = New GridTableView(RadGrid1)
        Dim childGrid As New RadGrid()
        Dim boundColumn As GridBoundColumn
 
        For ndlCount As Integer = 0 To ndl.Count - 1 Step 1
            Dim dsChild As New DataSet
            Dim attrCount As Integer = 0
            If ndl(ndlCount).ChildNodes.Count > 0 Then
 
                'Dim gridtempCol As GridTemplateColumn = New GridTemplateColumn()
                'gridtempCol.DataField = "test"
                'gridtempCol.HeaderText = "test"
                'gridtempCol.ItemTemplate.InstantiateIn(childGrid)
                'RadGrid1.MasterTableView.TemplateControl.LoadTemplate(gridtempCol.ToString())
 
                Dim ndSelect As XmlNodeList = xmlDoc.SelectNodes("//Product/node()[@RowID=" & ndlCount + 1 & "]")
                Dim ndSelectNode As XmlNode = xmlDoc.SelectSingleNode("//Product/node()[@RowID=" & ndlCount + 1 & "]")
 
                tableView.AutoGenerateColumns = False
 
                'dsChild.ReadXml(New StringReader(ndSelectNode.InnerXml))
                'childGrid.DataSource = dsChild
 
                'childGrid.DataBind()
 
                For Each tableViewNode As XmlNode In ndl(ndlCount).ChildNodes
 
                    For attrCount = 0 To tableViewNode.Attributes.Count - 1 Step 1
                        boundColumn = New GridBoundColumn
                        boundColumn.DataField = tableViewNode.Attributes(attrCount).Name
                        boundColumn.AllowSorting = True
                        boundColumn.ShowSortIcon = True
 
                        boundColumn.HeaderText = tableViewNode.Attributes(attrCount).Name
                        tableView.Columns.Add(boundColumn)
                    Next
                    tableView.DataBind()
                Next
                ' tableView.DataSource = dsChild
            End If
        Next
        ' tableView.Controls.Add(childGrid)
        RadGrid1.MasterTableView.DetailTables.Add(tableView)
 
        RadGrid1.DataBind()
 
        Session.Add("dt", dt)
        Session.Add("ctrlListing", RadGrid1)
        Return dt
 
    End Function
 
End Class

1 Answer, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 13 Aug 2010, 08:20 AM
Hello smith spd,

The knowledge base article you are referring to shows you need to use Page_Init to define your grid structure and the NeedDataSource and DetailTableDataBind events to bind the grid and its nested tables.

To demonstrate this approach with your provided XML structure, you can find attached a small test page. Note the NeedDataSource event where I get the required table by name ("Items"). Also note the DetailTableDatabind event where I need to find the index of the parent Item record. I have the ParentID field value from the parent grid item, but the DataTable "Description" gives me the sequential index of the related Items record. Therefore, I need to first loop over all the items in the Items table to find the index of the item that has the specified ParentID. Then I can use the index to filter the Description table.

Regards,
Veli
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
smith spd
Top achievements
Rank 1
Answers by
Veli
Telerik team
Share this question
or