473,802 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3851
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="s qldsMaster"
OnItemDataBound ="rpt_ItemDataB ound">
<HeaderTemplate >
<table>
<tr>
<td>Company Name</td>
<td>Contact Name</td>
<td>Contact Title</td>
</tr>
</HeaderTemplate>
<ItemTemplate >
<tr>
<td><%# Eval("CompanyNa me") %></td>
<td><%# Eval("ContactNa me") %></td>
<td><%# Eval("ContactTi tle") %></td>
</tr>
<tr>
<td colspan="3">
<asp:Label Visible="false" runat="server"
ID="lblCustID" Text='<%# Eval("CustomerI D") %>'></asp:Label>
<asp:GridView runat="server" ID="grdDetails "
AutoGenerateCol umns="true">
</asp:GridView>
</td>
</tr>
</ItemTemplate>
<FooterTemplate >
</table>
</FooterTemplate>
</asp:Repeater>
<asp:SqlDataSou rce ID="sqldsMaster " runat="server"
ConnectionStrin g="<%$ ConnectionStrin gs:NorthwindCon nectionString %>"
SelectCommand=" SELECT [CustomerID], [CompanyName],
[ContactName], [ContactTitle] FROM [Customers]">
</asp:SqlDataSour ce>
</form>
<script runat="server">
public void rpt_ItemDataBou nd(object sender, RepeaterItemEve ntArgs e)
{
if (e.Item.ItemTyp e==ListItemType .Item ||
e.Item.ItemType ==ListItemType. AlternatingItem )
{
string custID= (e.Item.DataIte m as
System.Data.Dat aRowView)["CustomerID "].ToString();
GridView gv=(GridView) e.Item.FindCont rol("grdDetails ");
gv.DataSource = GetCustomerOrde rs(custID);
gv.DataBind();
}

}
public System.Data.Sql Client.SqlDataR eader GetCustomerOrde rs(string
custID)
{
System.Data.Sql Client.SqlConne ction con=new
System.Data.Sql Client.SqlConne ction
(System.Configu ration.Configur ationManager.Co nnectionStrings["NorthwindConne ctionString"].ConnectionStri ng);
con.Open();
System.Data.Sql Client.SqlComma nd com=new
System.Data.Sql Client.SqlComma nd("Select OrderID,OrderDa te From Orders where
CustomerID=@Cus tomerID");
com.Connection= con;
com.Parameters. AddWithValue("@ CustomerID",cus tID);
return
com.ExecuteRead er(System.Data. CommandBehavior .CloseConnectio n);
}
</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="s qldsMaster"
OnItemDataBound ="rpt_ItemDataB ound">
<HeaderTemplate >
<table>
<tr>
<td>Company Name</td>
<td>Contact Name</td>
<td>Contact Title</td>
</tr>
</HeaderTemplate>
<ItemTemplate >
<tr>
<td><%# Eval("CompanyNa me") %></td>
<td><%# Eval("ContactNa me") %></td>
<td><%# Eval("ContactTi tle") %></td>
</tr>
<tr>
<td colspan="3">
<asp:Label Visible="false" runat="server"
ID="lblCustID" Text='<%# Eval("CustomerI D") %>'></asp:Label>
<asp:GridView runat="server" ID="grdDetails "
AutoGenerateCol umns="true">
</asp:GridView>
</td>
</tr>
</ItemTemplate>
<FooterTemplate >
</table>
</FooterTemplate>
</asp:Repeater>
<asp:SqlDataSou rce ID="sqldsMaster " runat="server"
ConnectionStrin g="<%$ ConnectionStrin gs:NorthwindCon nectionString %>"
SelectCommand=" SELECT [CustomerID], [CompanyName],
[ContactName], [ContactTitle] FROM [Customers]">
</asp:SqlDataSour ce>
</form>
<script runat="server">
public void rpt_ItemDataBou nd(object sender, RepeaterItemEve ntArgs e)
{
if (e.Item.ItemTyp e==ListItemType .Item ||
e.Item.ItemType ==ListItemType. AlternatingItem )
{
string custID= (e.Item.DataIte m as
System.Data.Dat aRowView)["CustomerID "].ToString();
GridView gv=(GridView) e.Item.FindCont rol("grdDetails ");
gv.DataSource = GetCustomerOrde rs(custID);
gv.DataBind();
}

}
public System.Data.Sql Client.SqlDataR eader GetCustomerOrde rs(string
custID)
{
System.Data.Sql Client.SqlConne ction con=new
System.Data.Sql Client.SqlConne ction
(System.Configu ration.Configur ationManager.Co nnectionStrings["NorthwindConne ctionString"].ConnectionStri ng);
con.Open();
System.Data.Sql Client.SqlComma nd com=new
System.Data.Sql Client.SqlComma nd("Select OrderID,OrderDa te From Orders where
CustomerID=@Cus tomerID");
com.Connection= con;
com.Parameters. AddWithValue("@ CustomerID",cus tID);
return
com.ExecuteRead er(System.Data. CommandBehavior .CloseConnectio n);
}
</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
5677
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 GridView to allow entry of quantity and Y/N switch for
6
3041
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 current_row_key of Grid1... to the objectDataSource2 parameter? (so that the second grid, gets only the information to do with current row of grid1)
5
4842
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 automatically if the data doesn't change. But if records have been added or deleted then it looks as if some code is necessary: I've done this by using GridView.SelectedValue to get the key value of the currently selected Row and then by itterating...
5
2993
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 display, update and modify data in a one to many relationship. Is there any resource available that can show me how a web form looks when the data is in a one to many relationship. I've done work in MS Access and it was very easy using sub forms...
2
433
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 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...
0
1526
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 different set of keys than the one used for the edit. The datakeyname in my gridview has a key I need for editing that I do not need for deleting. The delete gives me the Procedure or function has too many arguments specified error so long...
2
13203
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 fields to be included in the table, which is to be bound to the gridview. The web form looks like so (Don't worry about the stupidity of this web form for now.):
1
1446
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 each car) I want to show just 1 picture from the "CarPictures" table. Any suggestions as to how to do this?
0
1984
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 that every works really fine with this code when customers order approximatelly a maximum of 35 products. What I learned so far is to set the EnableViewState="False" for all the labels that don't need a viewstate. This helped a lot. What can I do...
3
5247
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 DataTable, I am using List of Objects. Here's one example: http://ryanolshan.com/technology/gridview-without-datasourcecontrol-datasource/
0
9699
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10536
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10063
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9114
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6838
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.