Hierarchical data and Gridview

Discussion in 'ASP .Net' started by =?Utf-8?B?Q29uZ2Vybw==?=, Mar 13, 2006.

  1. 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
    --
    Live long, stay strong
     
    =?Utf-8?B?Q29uZ2Vybw==?=, Mar 13, 2006
    #1
    1. Advertising

  2. 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" wrote:

    > 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
    > --
    > Live long, stay strong
     
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Mar 13, 2006
    #2
    1. Advertising

  3. 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" <> escribió en el mensaje
    news:D...
    > 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" wrote:
    >
    >> 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
    >> --
    >> Live long, stay strong
     
    Don Quijote de Nicaragua, Mar 13, 2006
    #3
  4. 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" wrote:

    > 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" <> escribió en el mensaje
    > news:D...
    > > 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" wrote:
    > >
    > >> 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
    > >> --
    > >> Live long, stay strong

    >
    >
    >
     
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Mar 14, 2006
    #4
  5. Thank you very much, I am going to study it
    Don Quijote de Nicaragua.
    Elder Soto.


    "Phillip Williams" <> escribió en el mensaje
    news:...
    > 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" wrote:
    >
    >> 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" <> escribió en el mensaje
    >> news:D...
    >> > 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" wrote:
    >> >
    >> >> 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
    >> >> --
    >> >> Live long, stay strong

    >>
    >>
    >>
     
    Don Quijote de Nicaragua, Mar 14, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. neverstill

    Binding hierarchical data

    neverstill, Dec 8, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    1,208
    Anders Borum
    Dec 9, 2003
  2. Don Quijote de Nicaragua

    GridView Hierarchical + VS.2005

    Don Quijote de Nicaragua, Nov 18, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    782
    Don Quijote de Nicaragua
    Nov 18, 2005
  3. =?Utf-8?B?bWdvbnphbGVzMw==?=

    GridView Hierarchical View - Gridview in Gridview

    =?Utf-8?B?bWdvbnphbGVzMw==?=, May 9, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    18,008
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    May 9, 2006
  4. Guabble
    Replies:
    6
    Views:
    703
    Guabble
    Feb 5, 2007
  5. bclegg

    Template controls and hierarchical data

    bclegg, Feb 5, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    141
    Steven Cheng[MSFT]
    Feb 7, 2004
Loading...

Share This Page