Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Making Connections: Binding Controls to Custom Data Sources in .NET : Page 3

Anyone can bind a DataGrid to a DataSet in .NET, but the fact that you can bind almost any control property to almost any suitable object property—even to custom objects or custom object collections—is far less widely known.


advertisement
Binding to Hierarchical Data
One common programming scenario is a requirement to display data contained in a hierarchy of objects. The root of the hierarchy may be a single object or collection of objects. Each root object then contains one or more children, and so on.

The Windows Forms DataGrid control understands hierarchical data and has built-in properties and methods that allow users to navigate through the object tree. The ASP.NET Grid does not make life that easy. You have to add a Grid to each of the columns containing a collection of objects. The Visual Studio IDE cannot currently cope with this more complex arrangement, so you have to code it yourself.

The following ASP example shows how to build a simple hierarchical display of the data using multiple DataGrid controls. This example uses a two-level hierarchy but you can extend the principle to any desirable depth.

In this example, the hierarchy consists of two objects: a Customer object and a strongly typed collection of that customer's Orders. Here's the ASP code:

<form id="Form1" method="post" runat="server"> <asp:DataGrid id=DataGrid1 runat="server" DataSource='<%# Customers %>' AutoGenerateColumns="False"> <Columns> <asp:BoundColumn DataField="Name" HeaderText="Name"></asp:BoundColumn> <asp:TemplateColumn HeaderText="Orders"> <ItemTemplate> <asp:DataGrid id="Datagrid2" runat="server" DataSource= '<%# DataBinder.Eval( Container, "DataItem.Orders") %>'> </asp:DataGrid> </ItemTemplate> <EditItemTemplate> <asp:DataGrid id="Datagrid3" runat="server" DataSource='<%# DataBinder.Eval( Container, "DataItem.Orders") %>'> </asp:DataGrid> </EditItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid> </form>

Here's the code-behind page that supplies the data.

public class TreeGrid : System.Web.UI.Page { protected System.Web.UI.WebControls.DataGrid DataGrid1; public TreeGrid() { Customers = new CustomerCollection(); Customers.Add(new Customer("Graham", "Brooks", "graham@agilethought.co.uk")); Customers.Add(new Customer("A", "Customer", "acustomer@somedomain.com")); } public CustomerCollection Customers; private void Page_Load(object sender, System.EventArgs e) { if (!IsPostBack) { DataBind(); } } #region Web Form Designer generated code #endregion }

The page consists of two grids, one nested inside the other and defined as a template column. When you bind the outer grid to a Customer object the inner grid gets bound to the order collection property of the customer object using the DataBinder.Eval expression. The inner grid then iterates over the order collection displaying the properties of the order.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date