473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

1 to many in GridView

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!

Sep 16 '06 #1
2 3825
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.Connect ionStrings["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.Clos eConnection);
}
</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!

Sep 17 '06 #2
MRW
These two examples are great and worked! Thank you very much!

Mohamed Mosalem wrote:
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.Connect ionStrings["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.Clos eConnection);
}
</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!
Sep 17 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: theKirk | last post by:
using Visual Studio 2005 C# ASP.NET I know there has to be a simple way to do this....I want to use C# in a code behind for aspx. Populate a GridView from an xml file Add Fields to the...
6
by: Nalaka | last post by:
Hi, I have a gridView (grid1), which as a templateColumn. In the template column, I have put in a gridView (grid2) and a ObjectDataSource (objectDataSource2). Question is... How to I pass the...
5
by: Dick | last post by:
I have a GridView bound to an ObjectDataSource. I have a Button that calls GridView.DataBind. I want the row that is selected before the DataBind to still be selected afterwards. This happens...
5
by: Robert | last post by:
I am very new to .net and asp.net. I am trying to design an intranet page that has a SQL Server 2000 database. I understand how to connect to the data but am getting very confused on how to...
2
by: MRW | last post by:
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...
0
by: jobs | last post by:
I have a gridview that I use to both select a record to be edited in a formview and to also delete records with. Because the selectcommand is grouping rows, my deletes stored procedure requires a...
2
by: antonyliu2002 | last post by:
I've been googling for some time, and could not find the solution to this problem. I am testing the paging feature of gridview. I have a very simple web form on which the user can select a few...
1
by: Cirene | last post by:
I have a gridview that displays a listing of cars for sale. It's based on a "Cars" table. (The "Cars" table has a 1 to many relationship to "CarPictures" table.) On each gridview row (ie for...
0
by: KeesH | last post by:
Hi, I am quite desperate now because I made a nice webshop with a gridview where the customer is able to update the amount of products (Hostas in my situation) or delete the row. The problem is...
3
by: Peter | last post by:
I have a GridView which is populated by List<ofObjects> Does anyone have example of how to sort the columns of this GridView? I have found examples without DataSourceControl but these use...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.