Showing hierarchical data with a Gridview

G

Guabble

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
 
G

Guabble

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
 
G

Guest

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();
}
}
 
G

Guabble

Adrian

Sorry to bug you but what is a DataView object?

cheers

mike

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\
 
G

Guabble

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
 

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

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top