Showing hierarchical data with a Gridview

Discussion in 'ASP .Net' started by Guabble, Feb 5, 2007.

  1. Guabble

    Guabble Guest

    Hi

    Can anyone help me? I want to be able to show my sqldatareader
    contents in a gridview whereby the child data is concatenated up in a
    single row. Is this possible?

    For example
    Author Name Book Titles
    Author1 Book1, Book2, Book3

    Author2 Book 4, Book7



    I can get it so that it binds, but displays a separate row of each
    Book name. Which is rubbish. I believe, as a alternative to my
    desired result, I can create like a little nested gridview instead of
    the Boot Titles column, which when click displays a list of titles.
    Is this possible? Or did I just dream it? Any ideas how to show this
    child data?

    Thanks for any help regarding this?
    cheers, Mike
    Guabble, Feb 5, 2007
    #1
    1. Advertising

  2. Try using a repeater control for your child data.
    =?Utf-8?B?QWRyaWFuIEpvbmVz?=, Feb 5, 2007
    #2
    1. Advertising

  3. Guabble

    Guabble Guest

    On Feb 5, 12:13 pm, Adrian Jones
    <> wrote:
    > Try using a repeater control for your child data.


    How would that work, would I have to loop through the recordset rows,
    grabbing the book title, concatenating them and outputting them.

    Sorry i've never used a repeater before, from what I've read about
    them, I thought they were mainly used to provide custom formatting to
    grid cells.

    Do you have any examples by any chance

    cheers

    Mike
    Guabble, Feb 5, 2007
    #3
  4. Hi Mike,

    I'm a relative newbie to ASP.NET, so please excuse me if anything here is
    done a bit back to front. But I've just used something like this to solve
    something similiar to what you describe:

    <!-- ==========================================
    Gridview with repeater
    ========================================== -->

    <asp:GridView ID="gvParent" runat="server"
    AutoGenerateColumns="False"
    DataSourceID="sqlParent"
    DataKeyNames="ParentID"
    OnRowDataBound="gvParent_RowDataBound" >
    <Columns>
    <asp:TemplateField HeaderText="Description">
    <ItemTemplate>
    <asp:Label ID="lblDescription" runat="server"
    Text='<%# Eval("Description") %>' />
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Child Data ">
    <ItemTemplate>
    <asp:Repeater ID="rptChild" runat="server">
    <ItemTemplate>
    <asp:Label ID="lblChildDataField"
    runat="server"
    Text='<%# Eval("ChildDataField") %>'/>
    </ItemTemplate>
    </asp:Repeater>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>

    <!-- =================================================
    SQL sources for Parent and Child
    ================================================ -->

    <asp:SqlDataSource ID="sqlChild" runat="server" ConnectionString="<%$
    ConnectionStrings:connString %>"
    SelectCommand="GetChildData" SelectCommandType="StoredProcedure">
    <SelectParameters>
    <asp:ControlParameter ControlID="gvParent" Name="ParentID" Type="Int32" />
    </SelectParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="sqlParent" runat="server"
    ConnectionString="<%$ ConnectionStrings:connString %>"
    SelectCommand="GetParentData" SelectCommandType="StoredProcedure">
    </asp:SqlDataSource>


    <-- In code-behind to databind each set of child data when parent is databound
    ================================================= -->

    protected void gvParent_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    Repeater rep = (Repeater)e.Row.FindControl("rptChild");
    string ID =
    gvParent.DataKeys[e.Row.DataItemIndex].Value.ToString();
    sqlChild.SelectParameters[0].DefaultValue = ID;
    DataView dv = (DataView)sqlChild.Select
    (DataSourceSelectArguments.Empty);
    rep.DataSource = dv;
    rep.DataBind();
    }
    }
    =?Utf-8?B?QWRyaWFuIEpvbmVz?=, Feb 5, 2007
    #4
  5. Guabble

    Guabble Guest

    cheers dude, will try it out.... thanks heaps
    Mike
    Guabble, Feb 5, 2007
    #5
  6. Guabble

    Guabble Guest

    Adrian

    Sorry to bug you but what is a DataView object?

    cheers

    mike

    On Feb 5, 1:25 pm, Adrian Jones
    <> wrote:
    > Hi Mike,
    >
    > I'm a relative newbie to ASP.NET, so please excuse me if anything here is
    > done a bit back to front. But I've just used something like this to solve
    > something similiar to what you describe:
    >
    > <!-- ==========================================
    > Gridview with repeater
    > ========================================== -->
    >
    > <asp:GridView ID="gvParent" runat="server"
    > AutoGenerateColumns="False"
    > DataSourceID="sqlParent"
    > DataKeyNames="ParentID"
    > OnRowDataBound="gvParent_RowDataBound" >
    > <Columns>
    > <asp:TemplateField HeaderText="Description">
    > <ItemTemplate>
    > <asp:Label ID="lblDescription" runat="server"
    > Text='<%# Eval("Description") %>' />
    > </ItemTemplate>
    > </asp:TemplateField>
    > <asp:TemplateField HeaderText="Child Data ">
    > <ItemTemplate>
    > <asp:Repeater ID="rptChild" runat="server">
    > <ItemTemplate>
    > <asp:Label ID="lblChildDataField"
    > runat="server"
    > Text='<%# Eval("ChildDataField") %>'/>
    > </ItemTemplate>
    > </asp:Repeater>
    > </ItemTemplate>
    > </asp:TemplateField>
    > </Columns>
    > </asp:GridView>
    >
    > <!-- =================================================
    > SQL sources for Parent and Child
    > ================================================ -->
    >
    > <asp:SqlDataSource ID="sqlChild" runat="server" ConnectionString="<%$
    > ConnectionStrings:connString %>"
    > SelectCommand="GetChildData" SelectCommandType="StoredProcedure">
    > <SelectParameters>
    > <asp:ControlParameter ControlID="gvParent" Name="ParentID" Type="Int32" />
    > </SelectParameters>
    > </asp:SqlDataSource>
    > <asp:SqlDataSource ID="sqlParent" runat="server"
    > ConnectionString="<%$ ConnectionStrings:connString %>"
    > SelectCommand="GetParentData" SelectCommandType="StoredProcedure">
    > </asp:SqlDataSource>
    >
    > <-- In code-behind to databind each set of child data when parent is databound
    > ================================================= -->
    >
    > protected void gvParent_RowDataBound(object sender, GridViewRowEventArgs e)
    > {
    > if (e.Row.RowType == DataControlRowType.DataRow)
    > {
    > Repeater rep = (Repeater)e.Row.FindControl("rptChild");
    > string ID =
    > gvParent.DataKeys[e.Row.DataItemIndex].Value.ToString();
    > sqlChild.SelectParameters[0].DefaultValue = ID;
    > DataView dv = (DataView)sqlChild.Select
    > (DataSourceSelectArguments.Empty);
    > rep.DataSource = dv;
    > rep.DataBind();
    > }
    >
    >
    >
    > }- Hide quoted text -
    >
    > - Show quoted text -


    d\
    Guabble, Feb 5, 2007
    #6
  7. Guabble

    Guabble Guest

    you da ma! Loving it. got it working, thanks very much. Didn;t know
    about dataviews. There seems there is so much in .net that you just
    dont know about!

    thanks v much
    Guabble, Feb 5, 2007
    #7
    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,200
    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:
    768
    Don Quijote de Nicaragua
    Nov 18, 2005
  3. =?Utf-8?B?Q29uZ2Vybw==?=

    Hierarchical data and Gridview

    =?Utf-8?B?Q29uZ2Vybw==?=, Mar 13, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    14,002
    Don Quijote de Nicaragua
    Mar 14, 2006
  4. Ray Booysen
    Replies:
    2
    Views:
    645
    Ray Booysen
    Mar 28, 2006
  5. =?Utf-8?B?bWdvbnphbGVzMw==?=

    GridView Hierarchical View - Gridview in Gridview

    =?Utf-8?B?bWdvbnphbGVzMw==?=, May 9, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    17,961
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    May 9, 2006
Loading...

Share This Page