Hierarchical Data Binding in ASP.NET

This will show a sample of hierarchical data bound to
hierarchical List Controls (repeaters)

 

This is just a transcript of the great article by Fritz Onion
from http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/aspn-hierdatabinding.asp

 

Get the data

This is obvious

 

//Create DataSet and Bind it to the
repeater

private
void Page_Load(object sender, EventArgs e)

{

     
string strConn
=

            “server=.;trusted_connection=yes;database=northwind”;

 

     
string strSql 
= “SELECT CustomerID, CompanyName FROM ” +

           
” Customers;
”                         +

           
“SELECT OrderID, CustomerID,
”         +

           
”        EmployeeID FROM
Orders;”      +

            “SELECT OrderID,
Products.ProductID,”  +

            “ProductName, Products.UnitPrice
FROM” +

            ” [Order Details], Products WHERE
”    +

           
” [Order Details].ProductID =
”        +

            “Products.ProductID”;

 

      SqlConnection conn = new SqlConnection(strConn);

      SqlDataAdapter da = new SqlDataAdapter(strSql, conn);

      da.TableMappings.Add(“Customers1″,
“Orders”);

      da.TableMappings.Add(“Customers2″,
“OrderDetails”);

 

     
//Fill DataSet

      _ds = new DataSet();

      da.Fill(_ds, “Customers”);

 

     
//Add Relations

      _ds.Relations.Add(“Customer_Order”,

            _ds.Tables["Customers"].Columns["CustomerID"],

            _ds.Tables["Orders"].Columns["CustomerID"]);

      _ds.Relations[0].Nested = true;

      _ds.Relations.Add(“Order_OrderDetail”,

            _ds.Tables["Orders"].Columns["OrderID"],

            _ds.Tables["OrderDetails"].Columns["OrderID"]);

      _ds.Relations[1].Nested = true;

 

     
//Bind

      _customerRepeater.DataSource =
_ds.Tables["Customers"];

      _customerRepeater.DataBind();

}

 

Very useful function

///<summary>

/// returns a DataView of ChildRows

///</summary>

///<param name=”dataItem”>parent row</param>

///<param name=”relation”>the name of the
relation
</param>

///<returns>DataView</returns>

protected DataView GetChildRelation(object dataItem,

     
string relation)

{

      DataRowView drv = dataItem as DataRowView;

     
if (drv != null)

           
return drv.CreateChildView(relation);

     
else

           
return
null;

}

 

HTML view

Three hierarchical repeaters:

 

<asp:RepeaterRunat=”server”ID=”_customerRepeater”

     
EnableViewState=”false”>

 
<ItemTemplate>

    Customer:

   
<%#
DataBinder.Eval(Container.DataItem, “CustomerID”) %>

  
&nbsp;
&nbsp;

   
<%#
DataBinder.Eval(Container.DataItem,”CompanyName”) %>

   
<br/>

   
<asp:Repeaterrunat=”server”EnableViewState=”false”

        
DataSource=

           
<%# GetChildRelation(Container.DataItem,

                                 
“Customer_Order”)%>

   
ID=”Repeater1″>

     
<itemTemplate>

      
&nbsp;&nbsp;&nbsp;&nbsp;

       Orderid:<b>

      
<%#DataBinder.Eval(Container.DataItem,
“OrderID”)%>

      
</b><br/>

      
<asp:Repeaterrunat=”server”EnableViewState=”false”

           
DataSource=

               
<%#
GetChildRelation(Container.DataItem,

                                    
“Order_OrderDetail”)%>

      
ID=”Repeater2″>

        
<itemTemplate>

          
&nbsp;&nbsp;&nbsp;&nbsp;

          
&nbsp;&nbsp;&nbsp;&nbsp;

          
<b><%# DataBinder.Eval(Container.DataItem,

                                 
“ProductName”) %></b>

           $<%#
DataBinder.Eval(Container.DataItem,

                   
            “UnitPrice”) %>
<br/>

        
</itemTemplate>

      
</asp:Repeater>

    
</itemTemplate>

  
</asp:Repeater>

 </ItemTemplate>

</asp:Repeater>

Leave a Reply