A simple demo that shows how to create a Master-Detail View in ASP.NET.
Creating the underlying classes
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
// Customer Classusing 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 Classusing 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 Classusing System.Collections;using System.Collections.Generic;public class Customers: List<Customer> { }//Orders Classusing System.Collections;using System.Collections.Generic;public class Orders: List<Order> { }
Setting up our GridView
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
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)
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.
E-mail
Theme design by Jelle Druyts