Hierarchical data and Gridview

G

Guest

I'm trying to find a way to bind hierarchical data to a gridview control.
I've been able to do this with some third party controls and was wondering if
this functionality is available with the gridview control. Does anyone have a
guidance on this? Thanks
 
G

Guest

You can use the XPath data binding expression within ItemTemplates. For
example, if you had an xml that looks like this:
<RootNode>
<Node>
<Category ID="1">
<Product ID="1" Name="Product 1">
<Supplier ID="1">Description for Supplier 1</Supplier>
<Supplier ID="2">Description for Supplier 2</Supplier>
</Product>
</Category>

</Node>
</RootNode>

and an XMLDataSource object that reads that xml like this:

<asp:XmlDataSource ID="XmlDataSourceControl1"
DataFile="~/App_Data/Products.xml"
XPath="RootNode/Node/Category[@ID='1']/Product" runat="Server" />

Then you can write a GridView with templates like this:

<asp:GridView ID="GridView1" runat="server" DataSourceID="XmlDataSource1"
AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
Product ID</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#XPath("@ID")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Product Name</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#XPath("Name")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Product Name</HeaderTemplate>
<ItemTemplate>
<asp:DataList id="SupplierDataList" DataSource='<%#
XPathSelect("Supplier") %>' runat="server">
<ItemTemplate>
<br>
<u>
Supplier ID <%# XPath("@ID") %>:
<%# XPath("@ID") %>
</u>
<br>
<%# XPath(".") %>
</ItemTemplate>
</asp:DataList>

</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
 
D

Don Quijote de Nicaragua

Hi Phillips, If my source of data is a table of Access or SQL from Access
for example

SELECT Group.Name, Product.Name,Product.Price FROM Gruop,Product where
Group.Code=Product.Code

How I could make in GridView Hierarchical

Group1
Product1 12.00
Product2 13.56
Product3 16.35
Thanks.
Don Quijote de Nicaragua.
Elder Soto.



Phillip Williams said:
You can use the XPath data binding expression within ItemTemplates. For
example, if you had an xml that looks like this:
<RootNode>
<Node>
<Category ID="1">
<Product ID="1" Name="Product 1">
<Supplier ID="1">Description for Supplier 1</Supplier>
<Supplier ID="2">Description for Supplier 2</Supplier>
</Product>
</Category>

</Node>
</RootNode>

and an XMLDataSource object that reads that xml like this:

<asp:XmlDataSource ID="XmlDataSourceControl1"
DataFile="~/App_Data/Products.xml"
XPath="RootNode/Node/Category[@ID='1']/Product" runat="Server" />

Then you can write a GridView with templates like this:

<asp:GridView ID="GridView1" runat="server" DataSourceID="XmlDataSource1"
AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
Product ID</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#XPath("@ID")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Product Name</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server"
Text='<%#XPath("Name")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Product Name</HeaderTemplate>
<ItemTemplate>
<asp:DataList id="SupplierDataList" DataSource='<%#
XPathSelect("Supplier") %>' runat="server">
<ItemTemplate>
<br>
<u>
Supplier ID <%# XPath("@ID") %>:
<%# XPath("@ID") %>
</u>
<br>
<%# XPath(".") %>
</ItemTemplate>
</asp:DataList>

</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Congero said:
I'm trying to find a way to bind hierarchical data to a gridview control.
I've been able to do this with some third party controls and was
wondering if
this functionality is available with the gridview control. Does anyone
have a
guidance on this? Thanks
 
G

Guest

If you are looking to represent a one-level hierarchy from a relational
datatable you might create 2 dataviews for the same table by querying again
the same table during the GridView.RowDataBound event handling to get a view
of the child records. I did a while ago a similar sample using the
DataGrid's ItemDataBound event. You might find it helpful in understanding
the concept and then you can create a similar implementation for the GridView:
http://www.societopia.net/Samples/DataGrid_Hierarchy.aspx

If your hierarchy can be more than one level then you would need to use
recursion like I did using the datagrid in this sample:
http://www.societopia.net/samples/webform2.aspx
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Don Quijote de Nicaragua said:
Hi Phillips, If my source of data is a table of Access or SQL from Access
for example

SELECT Group.Name, Product.Name,Product.Price FROM Gruop,Product where
Group.Code=Product.Code

How I could make in GridView Hierarchical

Group1
Product1 12.00
Product2 13.56
Product3 16.35
Thanks.
Don Quijote de Nicaragua.
Elder Soto.



Phillip Williams said:
You can use the XPath data binding expression within ItemTemplates. For
example, if you had an xml that looks like this:
<RootNode>
<Node>
<Category ID="1">
<Product ID="1" Name="Product 1">
<Supplier ID="1">Description for Supplier 1</Supplier>
<Supplier ID="2">Description for Supplier 2</Supplier>
</Product>
</Category>

</Node>
</RootNode>

and an XMLDataSource object that reads that xml like this:

<asp:XmlDataSource ID="XmlDataSourceControl1"
DataFile="~/App_Data/Products.xml"
XPath="RootNode/Node/Category[@ID='1']/Product" runat="Server" />

Then you can write a GridView with templates like this:

<asp:GridView ID="GridView1" runat="server" DataSourceID="XmlDataSource1"
AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
Product ID</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#XPath("@ID")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Product Name</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server"
Text='<%#XPath("Name")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Product Name</HeaderTemplate>
<ItemTemplate>
<asp:DataList id="SupplierDataList" DataSource='<%#
XPathSelect("Supplier") %>' runat="server">
<ItemTemplate>
<br>
<u>
Supplier ID <%# XPath("@ID") %>:
<%# XPath("@ID") %>
</u>
<br>
<%# XPath(".") %>
</ItemTemplate>
</asp:DataList>

</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Congero said:
I'm trying to find a way to bind hierarchical data to a gridview control.
I've been able to do this with some third party controls and was
wondering if
this functionality is available with the gridview control. Does anyone
have a
guidance on this? Thanks
 
D

Don Quijote de Nicaragua

Thank you very much, I am going to study it
Don Quijote de Nicaragua.
Elder Soto.


Phillip Williams said:
If you are looking to represent a one-level hierarchy from a relational
datatable you might create 2 dataviews for the same table by querying
again
the same table during the GridView.RowDataBound event handling to get a
view
of the child records. I did a while ago a similar sample using the
DataGrid's ItemDataBound event. You might find it helpful in
understanding
the concept and then you can create a similar implementation for the
GridView:
http://www.societopia.net/Samples/DataGrid_Hierarchy.aspx

If your hierarchy can be more than one level then you would need to use
recursion like I did using the datagrid in this sample:
http://www.societopia.net/samples/webform2.aspx
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Don Quijote de Nicaragua said:
Hi Phillips, If my source of data is a table of Access or SQL from Access
for example

SELECT Group.Name, Product.Name,Product.Price FROM Gruop,Product where
Group.Code=Product.Code

How I could make in GridView Hierarchical

Group1
Product1 12.00
Product2 13.56
Product3 16.35
Thanks.
Don Quijote de Nicaragua.
Elder Soto.



Phillip Williams said:
You can use the XPath data binding expression within ItemTemplates. For
example, if you had an xml that looks like this:
<RootNode>
<Node>
<Category ID="1">
<Product ID="1" Name="Product 1">
<Supplier ID="1">Description for Supplier 1</Supplier>
<Supplier ID="2">Description for Supplier 2</Supplier>
</Product>
</Category>

</Node>
</RootNode>

and an XMLDataSource object that reads that xml like this:

<asp:XmlDataSource ID="XmlDataSourceControl1"
DataFile="~/App_Data/Products.xml"
XPath="RootNode/Node/Category[@ID='1']/Product" runat="Server" />

Then you can write a GridView with templates like this:

<asp:GridView ID="GridView1" runat="server"
DataSourceID="XmlDataSource1"
AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
Product ID</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%#XPath("@ID")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Product Name</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server"
Text='<%#XPath("Name")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Product Name</HeaderTemplate>
<ItemTemplate>
<asp:DataList id="SupplierDataList" DataSource='<%#
XPathSelect("Supplier") %>' runat="server">
<ItemTemplate>
<br>
<u>
Supplier ID <%# XPath("@ID") %>:
<%# XPath("@ID") %>
</u>
<br>
<%# XPath(".") %>
</ItemTemplate>
</asp:DataList>

</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


:

I'm trying to find a way to bind hierarchical data to a gridview
control.
I've been able to do this with some third party controls and was
wondering if
this functionality is available with the gridview control. Does anyone
have a
guidance on this? Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top