none
How-to bind related datatables to treeview RRS feed

  • Question

  • I have three datatables in my dataset:

    Customers
    ID
    Name

    Items
    ID
    Name

    Orders
    ID
    CustomerID
    ItemID

    Also I have two datarelations:
    Customers.ID > Orders.CustomerID
    Items.ID > Orders.ItemID

    I'm trying to display data from these tables in a treeview control. So far I have success displaying customer names in a top level nodes and corresponding item IDs in child level nodes (see the source code below). What I want to do is display item names instead of items IDs. The question is how-to tell XAML to look up item name in the Items table by item ID in the Orders table.

    <Window x:Class="BindingTreeViewToDataSet.Window1" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:local="clr-namespace:BindingTreeViewToDataSet"   
        Title="Binding TreeView To DataSet"   
        Width="300" Height="300" 
        FontSize="13" 
        > 
      <Window.Resources> 
     
        <ObjectDataProvider   
          x:Key="dataSetProvider"   
          MethodName="CreateDataSet"   
          ObjectType="{x:Type local:DataSetCreator}"   
          /> 
     
        <DataTemplate x:Key="DetailTemplate">  
            <TextBlock Text="{Binding CustomerID}" /> 
        </DataTemplate> 
            
        <HierarchicalDataTemplate   
            x:Key="MasterTemplate"   
            ItemsSource="{Binding CustomersToOrders}"   
            ItemTemplate="{StaticResource DetailTemplate}" 
            > 
            <TextBlock Text="{Binding Name}" /> 
        </HierarchicalDataTemplate>      
      </Window.Resources> 
        
      <Grid> 
        <TreeView   
          DataContext="{StaticResource dataSetProvider}"   
          ItemsSource="{Binding Customers}" 
          ItemTemplate="{StaticResource MasterTemplate}"   
          /> 
      </Grid> 
    </Window> 
    Monday, February 16, 2009 2:41 PM

Answers

  • I build a demo project use code behind:
    XAML:
    <Window x:Class="_3TireTreeView.Window1" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="Window1" Height="300" Width="300"
        <Grid> 
            <TreeView x:Name="tree"
                 
            </TreeView> 
        </Grid> 
    </Window> 
     

    C#:
    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Windows; 
    using System.Windows.Controls; 
    using System.Windows.Data; 
    using System.Windows.Documents; 
    using System.Windows.Input; 
    using System.Windows.Media; 
    using System.Windows.Media.Imaging; 
    using System.Windows.Navigation; 
    using System.Windows.Shapes; 
    using System.Data; 
     
    namespace _3TireTreeView 
        /// <summary> 
        /// Interaction logic for Window1.xaml 
        /// </summary> 
        public partial class Window1 : Window 
        { 
            public DataSet data; 
            public Window1() 
            { 
                InitializeComponent(); 
                data = CreateData(); 
                LoadData(); 
            } 
     
            public DataSet CreateData() 
            { 
                #region 
                DataSet ds = new DataSet(); 
                DataTable dtCustom = new DataTable("Custom"); 
                dtCustom.Columns.Add("ID", typeof(string) ); 
                dtCustom.Columns.Add("NAME", typeof(string)); 
                DataRow dr; 
                dr = dtCustom.NewRow(); 
                dr["ID"] = "1"; 
                dr["NAME"] = "Tom"; 
                dtCustom.Rows.Add(dr); 
                dr = dtCustom.NewRow(); 
                dr["ID"] = "2"; 
                dr["NAME"] = "Jerry"; 
                dtCustom.Rows.Add(dr); 
     
                DataTable dtItems = new DataTable("Items"); 
                dtItems.Columns.Add("ID", typeof(string)); 
                dtItems.Columns.Add("NAME", typeof(string)); 
                dr = dtItems.NewRow(); 
                dr["ID"] = "1"; 
                dr["NAME"] = "Car"; 
                dtItems.Rows.Add(dr); 
                dr = dtItems.NewRow(); 
                dr["ID"] = "2"; 
                dr["NAME"] = "Bicycle"; 
                dtItems.Rows.Add(dr); 
     
                DataTable dtOrders = new DataTable("Orders"); 
                dtOrders.Columns.Add("ID", typeof(string)); 
                dtOrders.Columns.Add("CustomID", typeof(string)); 
                dtOrders.Columns.Add("ItemID", typeof(string)); 
                dr = dtOrders.NewRow(); 
                dr["ID"] = "1"; 
                dr["CustomID"] = "1"; 
                dr["ItemID"] = "1"; 
                dtOrders.Rows.Add(dr); 
        
                dr = dtOrders.NewRow(); 
                dr["ID"] = "2"; 
                dr["CustomID"] = "1"; 
                dr["ItemID"] = "2"; 
                dtOrders.Rows.Add(dr); 
                dr = dtOrders.NewRow(); 
                dr["ID"] = "3"; 
                dr["CustomID"] = "2"; 
                dr["ItemID"] = "1"; 
                dtOrders.Rows.Add(dr); 
                ds.Tables.Add(dtCustom); 
                ds.Tables.Add(dtItems); 
                ds.Tables.Add(dtOrders); 
                ds.Relations.Add("rs_CustomOrder", ds.Tables["Custom"].Columns["ID"] , ds.Tables["Orders"].Columns["CustomID"]); 
                ds.Relations.Add("rs_ItemsOrder", ds.Tables["Items"].Columns["ID"], ds.Tables["Orders"].Columns["ItemID"]); 
               
                 
                return ds; 
                #endregion 
            } 
     
      
     
            public void LoadData() 
            { 
                foreach (DataRow dr in data.Tables["Items"].Rows ) 
                { 
                    TreeViewItem root = new TreeViewItem(); 
                    root.Header = dr["NAME"].ToString(); 
                    tree.Items.Add(root); 
                    ExpandCustom(dr, root); 
                } 
            } 
     
            public void ExpandCustom(DataRow dr, TreeViewItem pNode) 
            { 
                
     
                foreach (DataRow row in dr.GetChildRows("rs_ItemsOrder")) 
                { 
                    TreeViewItem cChild = new TreeViewItem(); 
                    dr.GetChildRows("rs_ItemsOrder"); 
                    string customID = row["CustomID"].ToString(); 
                    string itemID = dr["ID"].ToString(); 
                    cChild.Header = GetCustomName(customID); 
                    pNode.Items.Add(cChild); 
                    ExpandOrder(customID, itemID, cChild); 
                } 
            } 
     
            public void ExpandOrder(string customID, string itemID, TreeViewItem pNode ) 
            { 
                DataRow[] drs = data.Tables["Orders"].Select(" CustomID='" + customID + "'" + "  and ItemID = '" + itemID + "' ");             
                for (int i = 1; i <= drs.Length; i++) 
                { 
                    TreeViewItem cChild = new TreeViewItem(); 
                    cChild.Header = "order" + i.ToString(); 
                    pNode.Items.Add(cChild); 
                } 
             
            } 
     
            public string GetCustomName( string id ) 
            { 
                string name = ""
                DataRow[] drs = data.Tables["Custom"].Select( "id='"+id+"'" ); 
                if (drs.Length > 0) 
                { 
                    name = drs[0]["NAME"].ToString(); 
                } 
                return name; 
               
            } 
        } 
     

    • Proposed as answer by Tao Liang Friday, February 20, 2009 9:07 AM
    • Marked as answer by Tao Liang Monday, February 23, 2009 2:05 AM
    Friday, February 20, 2009 9:06 AM