Hi aginell4life,
Using this article i have created the example and didn't find any issue with special characters.
Populate TreeView from Database in Windows Forms using C# and VB.Net
Database
I have used following Vehicle Access Database to populate the TreeView.
VehicleTypes

VehicleSubTypes

Namespaces
C#
using System.Data;
using System.Data.OleDb;
VB.Net
Imports System.Data
Imports System.Data.OleDb
Code
C#
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = this.GetData("SELECT Id, Name FROM VehicleTypes");
this.PopulateTreeView(dt, 0, null);
}
private void PopulateTreeView(DataTable dtParent, int parentId, TreeNode treeNode)
{
foreach (DataRow row in dtParent.Rows)
{
TreeNode child = new TreeNode
{
Text = row["Name"].ToString(),
Tag = row["Id"]
};
if (parentId == 0)
{
treeView1.Nodes.Add(child);
DataTable dtChild = this.GetData("SELECT Id, Name FROM VehicleSubTypes WHERE VehicleTypeId = " + child.Tag);
PopulateTreeView(dtChild, Convert.ToInt32(child.Tag), child);
}
else
{
treeNode.Nodes.Add(child);
}
}
}
private DataTable GetData(string query)
{
string constr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Dharmendra\Desktop\Vehicle.accdb";
using (OleDbConnection con = new OleDbConnection(constr))
{
using (OleDbDataAdapter sda = new OleDbDataAdapter(query, con))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim dt As DataTable = Me.GetData("SELECT Id, Name FROM VehicleTypes")
Me.PopulateTreeView(dt, 0, Nothing)
End Sub
Private Sub PopulateTreeView(dtParent As DataTable, parentId As Integer, treeNode As TreeNode)
For Each row As DataRow In dtParent.Rows
Dim child As New TreeNode() With {
.Text = row("Name").ToString(),
.Tag = row("Id")
}
If parentId = 0 Then
treeView1.Nodes.Add(child)
Dim dtChild As DataTable = Me.GetData("SELECT Id, Name FROM VehicleSubTypes WHERE VehicleTypeId = " & child.Tag)
PopulateTreeView(dtChild, Convert.ToInt32(child.Tag), child)
Else
treeNode.Nodes.Add(child)
End If
Next
End Sub
Private Function GetData(query As String) As DataTable
Dim constr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Dharmendra\Desktop\Vehicle.accdb"
Using con As New OleDbConnection(constr)
Using sda As New OleDbDataAdapter(query, con)
Using dt As New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Screenshot
