Does WPF TreeView work with ADO.NET DataTable? RRS feed

  • Question

  • Hi All!

    after doing some experimentation, and posting on WPF forum:

    I came to the conclusion that WPF TreeView control works incorrecly with ADO.NET DataTable for some unknown reason.

    I have TreeView control bound to a DataTable. If I update rows in the DataTable then only first-level items of TreeView are updated. Here is the complete example:

    <Window x:Class="WpfApplication1.Window1"
        Title="Window1" Height="331" Width="319" Loaded="Window_Loaded">
            <HierarchicalDataTemplate x:Key="ProjectTemplate" 
                                      DataType="{x:Type data:DataRowView}" 
                                      ItemsSource="{Binding Path=ProjectProject, 
                    <TextBlock Margin="0,3,0,0">
                                        <MultiBinding StringFormat="{}{0} (ProjectId={1})">
                                            <Binding Path="Name" />
                                            <Binding Path="ProjectId" />
            <ListBox Name="listbox" Height="100" VerticalAlignment="Top">
                    <DataTemplate DataType="{x:Type data:DataRowView}">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Name}" />
                            <TextBlock Text="   "/>
                            <TextBlock Text="{Binding ProjectId}"/>
            <Button Click="Button_Click" Content="update data source" Margin="3,0,0,2" Height="47" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="134" />
            <TreeView ItemTemplate="{Binding Source={StaticResource ProjectTemplate}}" Name="treeView1" Margin="3,106,0,55">
            <Button Height="47" Margin="143,0,0,2" Name="button1" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="103" Click="button1_Click">Update second row</Button>

    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 WpfApplication1
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
            public Window1()
            DataSet myds = new DataSet();
            private void Button_Click(object sender, RoutedEventArgs e)
                myds.Tables["ProjectTable"].Rows[0][0] = "new value";
                myds.Tables["ProjectTable"].Rows.Add(new object[] { "cc", 3, 2 });
            private void Window_Loaded(object sender, RoutedEventArgs e)
                DataTable m_dt = new DataTable("ProjectTable");
                m_dt.Columns.Add("Name", typeof(string));
                m_dt.Columns.Add("ProjectId", typeof(int));
                m_dt.Columns.Add("ParentProjectId", typeof(int));
                DataColumn parentColumn =
                DataColumn childColumn =
                // Create DataRelation.
                DataRelation projectprojectRelation;
                projectprojectRelation = new DataRelation("ProjectProject",
                    parentColumn, childColumn, false);
                // Add the relation to the DataSet.
                m_dt.Rows.Add(new object[] { "aa", 1, null });
                m_dt.Rows.Add(new object[] { "bb", 2, 1 });
                CollectionViewSource projectViewSource = new CollectionViewSource();
                projectViewSource.Source = myds.Tables[0].DefaultView;
                listbox.ItemsSource = projectViewSource.View;
                treeView1.ItemsSource = projectViewSource.View;
            private void button1_Click(object sender, RoutedEventArgs e)
                myds.Tables["ProjectTable"].Rows[1][0] = "new value";
    could someone comment this?

    • Moved by Yichun_Feng Wednesday, September 30, 2009 1:53 AM (From:ADO.NET DataSet)
    Friday, September 25, 2009 4:11 PM

All replies

  • Hi Dmitriano,


    This issue may due to the design of TreeView and DataTable.

    Since your project is based on WPF controls, people who knows more about WPF will have better understanding of TreeView control.

    I’ll move this thread to WPF forum and keep track of it.



    Best Regards

    Yichun Feng

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, September 30, 2009 1:53 AM