Re: 1 to many in GridView
- From: "Ken Cox [Microsoft MVP]" <BANSPAMkjopc@xxxxxxxxxxxxxxxxx>
- Date: Sat, 16 Sep 2006 20:53:42 -0400
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>
"MRW" <mwinne1@xxxxxxxxx> wrote in message
news:1158430599.912740.163640@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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!
.
- References:
- 1 to many in GridView
- From: MRW
- 1 to many in GridView
- Prev by Date: Atlas Problem
- Next by Date: Re: asp.net configuration question
- Previous by thread: Re: 1 to many in GridView
- Next by thread: 1 to many in GridView
- Index(es):
Relevant Pages
|