Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Treeview in VB.net
    primarykey
    data
    text
    <p>I have been looking for a some working code for the last couple of days to set up a treeview in my WPF app to display some database data. I use VB as i am much more confident in it - which drastically limited the code examples out there. More examples i found used manually entered data to populate each node versus a database which was little help. I finally found some code which bound the each node to a query and related the query via a DataRelation. Great this should work perfect!</p> <p>The example used the Northwind database so I changed it to my database and plugged in the queries. To my shock the treeview populated fine except.... all the data is invisible.</p> <p>Here is the code: </p> <pre><code>Imports MySql.Data.MySqlClient Imports System.Data Imports System.ComponentModel Imports System.Xml Imports System.IO Class Window2 Dim connStr As String = "Server=127.0.0.1;Database=psdb;Uid=root;Pwd=;Connect Timeout=30;" Dim conn As New MySqlConnection(connStr) Function GetRelationalData() As DataSet Dim CategoryAdapter As MySqlDataAdapter = New MySqlDataAdapter("select distinct Dist_name, dist_id from distributors".ToString, conn) Dim ProductsAdapter As MySqlDataAdapter = New MySqlDataAdapter("select d.dist_id, t.Title_name, title_id from titles t, distributors d where d.dist_id = t.dist_id".ToString, conn) Dim ProductData As DataSet = New DataSet() CategoryAdapter.Fill(ProductData, "Categories") 'fill Categories ProductsAdapter.Fill(ProductData, "Products") 'fill products Dim CategoryRelation As DataRelation CategoryRelation = New DataRelation("ChildrenRelationship", _ ProductData.Tables("Categories").Columns("dist_id"), _ ProductData.Tables("Products").Columns("dist_id"), True) CategoryRelation.Nested = True ProductData.Relations.Add(CategoryRelation) Return ProductData End Function Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As RoutedEventArgs) Handles Button1.Click BindData() End Sub Private Sub BindData() TreeView1.DataContext = GetRelationalData() End Sub End Class </code></pre> <p>and the XAML:</p> <pre><code>&lt;Window x:Class="Window2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="400" Width="550"&gt;&lt;Window.Resources&gt; &lt;DataTemplate x:Key="ProductTemplate" &gt; &lt;TextBlock Text="{Binding ProductName}"&gt;&lt;/TextBlock&gt; &lt;/DataTemplate&gt; &lt;HierarchicalDataTemplate x:Key="CategoryTemplate" ItemsSource="{Binding ChildrenRelationship}" ItemTemplate="{StaticResource ProductTemplate}"&gt; &lt;TextBlock Text="{Binding CategoryName}"&gt;&lt;/TextBlock&gt; &lt;/HierarchicalDataTemplate&gt; &lt;/Window.Resources&gt; &lt;Grid&gt; </code></pre> <p>and here what i end up with the data seems to be there but you can't see it. Help!</p> <p><img src="https://i.stack.imgur.com/KH3lu.jpg" alt="enter image description here"></p> <p>PS there may be some confusion as to the objects. I went back tried to keep as close to the original code as possible when i discovered the problem only replacing the queries with my own - the original code used "Categories" and "Products" where as I "use Distributors" and "Titles"</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload