How To: Master-Detail sample using ASP.NET with Generics#

A simple demo that shows how to create a Master-Detail View in ASP.NET.

Creating the underlying classes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Customer Class
using System;

public class Customer
{
private int _customerId;
public int CustomerId
{
get { return _customerId; }
set { _customerId = value; }
}

private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}

private string _address;
public string Address
{
get { return _address; }
set { _address = value; }
}

private string _cardType;
public string CardType
{
get { return _cardType; }
set { _cardType = value; }
}

private string _cardNumber;
public string CardNumber
{
get { return _cardNumber; }
set { _cardNumber = value; }
}

private Orders _orders;
public Orders Orders
{
get { return _orders; }
set { _orders = value; }
}
}

//Order Class
using System;

public class Order
{
private string _orderId;
public string OrderId
{
get { return _orderId; }
set { _orderId = value; }
}

private string _product;
public string Product
{
get { return _product; }
set { _product = value; }
}

private int _quantity;
public int Quantity
{
get { return _quantity; }
set { _quantity = value; }
}

private double _total;
public double Total
{
get { return _total; }
set { _total = value; }
}

private DateTime _orderDate;
public DateTime OrderDate
{
get { return _orderDate; }
set { _orderDate = value; }
}
}

//Customers Class
using System.Collections;
using System.Collections.Generic;


public class Customers: List<Customer> { }

//Orders Class
using System.Collections;
using System.Collections.Generic;


public class Orders: List<Order> { }

Setting up our GridView

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 
 <asp:GridView ID="customerGrid" runat="server" AutoGenerateColumns="False" 
      OnRowDataBound="customerGrid_RowDataBound" Width="70%" BackColor="White" 
      BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" 
      Font-Names="Arial" Font-Size="X-Small" ForeColor="Black" GridLines="Vertical">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="Id" >
<ItemStyle VerticalAlign="Top" />
</asp:BoundField>
<asp:BoundField DataField="Name" HeaderText="Customer Name" >
<ItemStyle VerticalAlign="Top" />
</asp:BoundField>
<asp:BoundField DataField="Address" HeaderText="Address" >
<ItemStyle VerticalAlign="Top" />
</asp:BoundField>
<asp:BoundField DataField="CardType" HeaderText="Card Type" >
<ItemStyle VerticalAlign="Top" />
</asp:BoundField>
<asp:BoundField DataField="CardNumber" HeaderText="Customer Number" >
<ItemStyle VerticalAlign="Top" />
</asp:BoundField>
<asp:TemplateField HeaderText="Orders">
<ItemTemplate>
<asp:GridView ID="ordersGrid" runat="server" Width="100%" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="OrderId" HeaderText="OrderId" >
<ItemStyle VerticalAlign="Top" />
</asp:BoundField>
<asp:BoundField DataField="Product" HeaderText="Product" >
<ItemStyle VerticalAlign="Top" />
</asp:BoundField>
<asp:BoundField DataField="Quantity" HeaderText="Quantity" >
<ItemStyle VerticalAlign="Top" />
</asp:BoundField>
<asp:BoundField DataField="Total" HeaderText="Total" >
<ItemStyle VerticalAlign="Top" />
</asp:BoundField>
<asp:BoundField DataField="OrderDate" HeaderText="OrderDate" >
<ItemStyle VerticalAlign="Top" />
</asp:BoundField>
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>

Putting everything together in our ASP.NET page.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
Customers customers = new Customers();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CreateCustomers();
customerGrid.DataSource = customers;
customerGrid.DataBind();
}
}

/// <summary>
/// Create dummy customers
/// </summary>
public void CreateCustomers()
{
//create 50 customers starting from 10000 to 10010
for (int customerId = 10000; customerId <= 10010; customerId++)
{
Customer customer = new Customer();
customer.CustomerId = customerId;
customer.Name = "Name " + customerId.ToString();
customer.Address = "Address" + customerId.ToString();
customer.CardType = "Visa" + customerId.ToString();
customer.CardNumber = "Card Number" + customerId.ToString();

//create an order for each customer
customer.Orders = CreateCustomerOrders(customerId);

//add the customer to our generic list
customers.Add(customer);
}
}

/// <summary>
/// Create dummy orders for each customer
/// </summary>
/// <param name="customerId">customer id</param>
/// <returns>a generic list of Orders</returns>
private Orders CreateCustomerOrders(int customerId)
{
Orders orders = new Orders();
for (int orderLine = 500; orderLine <= 505; orderLine++)
{
Order order = new Order();
order.OrderId = customerId + "-" + orderLine;
order.Product = "Product" + orderLine.ToString();
order.Quantity = orderLine - 450;
order.Total = 1.25 * order.Quantity;
order.OrderDate = DateTime.Now;
orders.Add(order);
}
return orders;
}
protected void customerGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the current row is of type DataRow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//find our orders grid
GridView ordersGrid = (GridView)e.Row.FindControl("ordersGrid");

//get the DataItem and cast it into a Customer class
Customer customer = (Customer)e.Row.DataItem;

//get our orders
ordersGrid.DataSource = customer.Orders;
//bind it!
ordersGrid.DataBind();
}
}
}

See it in action: http://demos.devpinoy.org/deepbinding/

or just download the project. KeithRull.DeepBinding.zip (4.99 KB)

Tuesday, December 26, 2006 10:52:18 PM (GMT Standard Time, UTC+00:00) #    Comments [0]  | 

 

Comments are closed.
All content © 2010, Keith Rull
On this page
This site
Calendar
<September 2010>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789
Archives
Sitemap
Blogroll OPML
Disclaimer

Powered by: newtelligence dasBlog 2.3.9074.18820

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Send mail to the author(s) E-mail

Theme design by Jelle Druyts


Pick a theme: