1 to many in GridView

  • Thread starter Thread starter MRW
  • Start date Start date
M

MRW

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!
 
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="sqldsMaster"
OnItemDataBound="rpt_ItemDataBound">
<HeaderTemplate>
<table>
<tr>
<td>Company Name</td>
<td>Contact Name</td>
<td>Contact Title</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("CompanyName") %></td>
<td><%# Eval("ContactName") %></td>
<td><%# Eval("ContactTitle") %></td>
</tr>
<tr>
<td colspan="3">
<asp:Label Visible="false" runat="server"
ID="lblCustID" Text='<%# Eval("CustomerID") %>'></asp:Label>
<asp:GridView runat="server" ID="grdDetails"
AutoGenerateColumns="true">
</asp:GridView>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="sqldsMaster" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [CustomerID], [CompanyName],
[ContactName], [ContactTitle] FROM [Customers]">
</asp:SqlDataSource>


</form>
<script runat="server">
public void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType==ListItemType.Item ||
e.Item.ItemType==ListItemType.AlternatingItem )
{
string custID= (e.Item.DataItem as
System.Data.DataRowView)["CustomerID"].ToString();
GridView gv=(GridView) e.Item.FindControl("grdDetails");
gv.DataSource = GetCustomerOrders(custID);
gv.DataBind();
}

}
public System.Data.SqlClient.SqlDataReader GetCustomerOrders(string
custID)
{
System.Data.SqlClient.SqlConnection con=new
System.Data.SqlClient.SqlConnection
(System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
con.Open();
System.Data.SqlClient.SqlCommand com=new
System.Data.SqlClient.SqlCommand("Select OrderID,OrderDate From Orders where
CustomerID=@CustomerID");
com.Connection=con;
com.Parameters.AddWithValue("@CustomerID",custID);
return
com.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
</script>
 
These two examples are great and worked! Thank you very much!

Mohamed said:
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="sqldsMaster"
OnItemDataBound="rpt_ItemDataBound">
<HeaderTemplate>
<table>
<tr>
<td>Company Name</td>
<td>Contact Name</td>
<td>Contact Title</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("CompanyName") %></td>
<td><%# Eval("ContactName") %></td>
<td><%# Eval("ContactTitle") %></td>
</tr>
<tr>
<td colspan="3">
<asp:Label Visible="false" runat="server"
ID="lblCustID" Text='<%# Eval("CustomerID") %>'></asp:Label>
<asp:GridView runat="server" ID="grdDetails"
AutoGenerateColumns="true">
</asp:GridView>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="sqldsMaster" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [CustomerID], [CompanyName],
[ContactName], [ContactTitle] FROM [Customers]">
</asp:SqlDataSource>


</form>
<script runat="server">
public void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType==ListItemType.Item ||
e.Item.ItemType==ListItemType.AlternatingItem )
{
string custID= (e.Item.DataItem as
System.Data.DataRowView)["CustomerID"].ToString();
GridView gv=(GridView) e.Item.FindControl("grdDetails");
gv.DataSource = GetCustomerOrders(custID);
gv.DataBind();
}

}
public System.Data.SqlClient.SqlDataReader GetCustomerOrders(string
custID)
{
System.Data.SqlClient.SqlConnection con=new
System.Data.SqlClient.SqlConnection
(System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
con.Open();
System.Data.SqlClient.SqlCommand com=new
System.Data.SqlClient.SqlCommand("Select OrderID,OrderDate From Orders where
CustomerID=@CustomerID");
com.Connection=con;
com.Parameters.AddWithValue("@CustomerID",custID);
return
com.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
</script>

MRW said:
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!
 
Back
Top