1 to many in GridView

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!
 
M

MRW

Sorry... quick addition. Instead of going with an inner join, I could
use a DataSet with the two related tables. The question is how to
display them properly.
 
K

Ken Cox [Microsoft MVP]

Hi,

You can include a gridview inside a gridview row by using the RowDataBound
event. Each time you loop through a row, get the key field and use that to
query the inner gridview. Here's an example that might get you started.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<%@ page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<script runat="server">

Protected Sub grdCustomers_RowDataBound _
(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim strcustomerID As String = _
DataBinder.Eval(e.Row.DataItem, "CustomerID")
Dim srcOrders As SqlDataSource = _
CType(e.Row.FindControl("SqlDataSource2"), SqlDataSource)
srcOrders.SelectParameters("CustomerID").DefaultValue = _
strcustomerID

End If
End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Nested Master/Detail</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:gridview id="grdCustomers"
runat="server"
autogeneratecolumns="False"
datasourceid="SqlDataSource1"
gridlines="None"
onrowdatabound="grdCustomers_RowDataBound"
showheader="False">
<columns>
<asp:templatefield>
<itemtemplate><br />
<h2 style="background-color: LightBlue; color: white"><%#
eval("companyname") %>(<%#Eval("CustomerID")%>)</h2>
<asp:gridview
id="grdOrders"
runat="server"
rowstyle-backcolor="white"
datasourceid="SqlDataSource2" />
<asp:sqldatasource id="SqlDataSource2"
runat="server"
connectionstring="<%$
ConnectionStrings:NorthwindConnectionString %>"
selectcommand="SELECT [CustomerID],[OrderID], [OrderDate],
[ShippedDate] FROM [Orders] WHERE ([CustomerID] = @CustomerID)">
<selectparameters>
<asp:parameter name="CustomerID" />
</selectparameters>
</asp:sqldatasource>
</itemtemplate>
</asp:templatefield>
</columns>
</asp:gridview>
<br />
<br />
<asp:sqldatasource
id="SqlDataSource1"
runat="server"
connectionstring="<%$
ConnectionStrings:NorthwindConnectionString %>"
selectcommand="SELECT [CustomerID], [CompanyName] FROM
[Customers]">
</asp:sqldatasource>
</div>
</form>
</body>
</html>
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top