Re: 1 to many in GridView

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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!



.



Relevant Pages

  • RE: GridView Multiple Select Buttons
    ... GridView row or more than one Select button per row? ... "Terry" wrote: ... "Phillip Williams" wrote: ...
    (microsoft.public.dotnet.framework.aspnet)
  • 1 to many in GridView
    ... GridView from table1 with a blue background, ... related records in table 2 in a white background, ...
    (microsoft.public.dotnet.framework.aspnet)
  • 1 to many in GridView
    ... GridView from table1 with a blue background, ... related records in table 2 in a white background, ...
    (microsoft.public.dotnet.framework.aspnet)
  • Displaying text from a gridview row bound to SQL server
    ... I was hoping to know how I could get the text value of a gridview row. ... //To bind data ... This is being able to bind and display data properly. ...
    (microsoft.public.dotnet.languages.csharp)
  • Displaying text from a gridview row bound to SQL server
    ... I was hoping to know how I could get the text value of a gridview row. ... //To bind data ... This is being able to bind and display data properly. ...
    (microsoft.public.dotnet.framework.aspnet)