Hi,
You can use a repeater control, within the item template of the repeater
display a row from the master table then add a gridview to display the
details, below a sample based on the northwind database that displays the
customers, one by one and below each customer the orders made by this customer
<form id="form1" runat="server">
<asp:Repeater runat="server" ID="rpt" DataSourceID="s qldsMaster"
OnItemDataBound ="rpt_ItemDataB ound">
<HeaderTemplate >
<table>
<tr>
<td>Company Name</td>
<td>Contact Name</td>
<td>Contact Title</td>
</tr>
</HeaderTemplate>
<ItemTemplate >
<tr>
<td><%# Eval("CompanyNa me") %></td>
<td><%# Eval("ContactNa me") %></td>
<td><%# Eval("ContactTi tle") %></td>
</tr>
<tr>
<td colspan="3">
<asp:Label Visible="false" runat="server"
ID="lblCustID" Text='<%# Eval("CustomerI D") %>'></asp:Label>
<asp:GridView runat="server" ID="grdDetails "
AutoGenerateCol umns="true">
</asp:GridView>
</td>
</tr>
</ItemTemplate>
<FooterTemplate >
</table>
</FooterTemplate>
</asp:Repeater>
<asp:SqlDataSou rce ID="sqldsMaster " runat="server"
ConnectionStrin g="<%$ ConnectionStrin gs:NorthwindCon nectionString %>"
SelectCommand=" SELECT [CustomerID], [CompanyName],
[ContactName], [ContactTitle] FROM [Customers]">
</asp:SqlDataSour ce>
</form>
<script runat="server">
public void rpt_ItemDataBou nd(object sender, RepeaterItemEve ntArgs e)
{
if (e.Item.ItemTyp e==ListItemType .Item ||
e.Item.ItemType ==ListItemType. AlternatingItem )
{
string custID= (e.Item.DataIte m as
System.Data.Dat aRowView)["CustomerID "].ToString();
GridView gv=(GridView) e.Item.FindCont rol("grdDetails ");
gv.DataSource = GetCustomerOrde rs(custID);
gv.DataBind();
}
}
public System.Data.Sql Client.SqlDataR eader GetCustomerOrde rs(string
custID)
{
System.Data.Sql Client.SqlConne ction con=new
System.Data.Sql Client.SqlConne ction
(System.Configu ration.Configur ationManager.Co nnectionStrings["NorthwindConne ctionString"].ConnectionStri ng);
con.Open();
System.Data.Sql Client.SqlComma nd com=new
System.Data.Sql Client.SqlComma nd("Select OrderID,OrderDa te From Orders where
CustomerID=@Cus tomerID");
com.Connection= con;
com.Parameters. AddWithValue("@ CustomerID",cus tID);
return
com.ExecuteRead er(System.Data. CommandBehavior .CloseConnectio n);
}
</script>
"MRW" wrote:
Hello!
I don't know if this is possible, but I have two tables in a
one-to-many relationship. I want one record to be displayed in a
GridView from table1 with a blue background, followed by all the
related records in table 2 in a white background, followed by the next
record in table1 in a blue background (for example). It should be nice
an simple. Back in the ASP Classic days, I would use response.write
and build a table, but I was wondering if there is an easier way to do
this in .NET. Oh yes, I'm using VB.
Thanks for any help!