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
Below is my code snippet:
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