Category / Section
How to populate WinForms TreeViewAdv from Datatable?
4 mins read
In WinForms TreeViewAdv there is no default support to bind DataTable and it can be achieved by loading TreeNodeAdv in iteration process from DataTable. This can be done by following below steps.
1. Need to initialize the DataTable with required data in Columns and Rows combination.
2. Need to integrate DataTable in DataSet and define Parent Child relationship.
3. Need to create TreeNodeAdv in iteration process from DataTable and load it in TreeViewAdv.
C#
// DataTable
DataTable dt = new DataTable("AddressLists");
//Use a DataSet to manage the data
DataSet ds = new DataSet();
//Add columns to the DataTable
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Parent", typeof(string));
//Add rows to the DataTable
dt.Rows.Add(new string[] { "Root", "0" });
dt.Rows.Add(new string[] { "Work", "Root" });
dt.Rows.Add(new string[] { "Home", "Root" });
dt.Rows.Add(new string[] { "One", "Work" });
dt.Rows.Add(new string[] { "Two", "Work" });
dt.Rows.Add(new string[] { "One", "Home" });
dt.Rows.Add(new string[] { "Two", "Home" });
dt.Rows.Add(new string[] { "Three", "Home" });
dt.Rows.Add(new string[] { "Other", "Root" });
//Add DataTable to the DataSet
ds.Tables.Add(dt);
//Add a relationship
ds.Relations.Add("TreeParentChild", ds.Tables["AddressLists"].Columns["Name"],
ds.Tables["AddressLists"].Columns["Parent"], false);
/// <summary>
/// To Populate TreeView
/// </summary>
public void PopulateTree(DataRow dr, TreeNodeAdv pNode)
{
//To iterate through all the rows in the DataSet
foreach (DataRow row in dr.GetChildRows("TreeParentChild"))
{
//Creating a TreeNode for each row
TreeNodeAdv cChild = new TreeNodeAdv(row["Name"].ToString());
//Add cChild node to the pNode
pNode.Nodes.Add(cChild);
//Recursively build the tree
PopulateTree(row, cChild);
}
}
/// <summary>
/// Button Click event
/// </summary>
private void buttonAdv1_Click(object sender, EventArgs e)
{
//Iterate all the rows in the DataSet
foreach (DataRow dr in ds.Tables["AddressLists"].Rows)
{
if(dr["Parent"].ToString() == "0")
{
//Creates a TreeNode if the parent equals 0
TreeNodeAdv root = new TreeNodeAdv(dr["Name"].ToString());
treeViewAdv1.Nodes.Add(root);
//Recursively builds the tree
PopulateTree(dr, root);
}
}
//Expands all the tree nodes
treeViewAdv1.ExpandAll();
}
VB.Net
' DataTable
Private dt As New DataTable("AddressLists")
'Use a DataSet to manage the data
Private ds As New DataSet()
'Add columns to the DataTable
dt.Columns.Add("Name", GetType(String))
dt.Columns.Add("Parent", GetType(String))
'Add rows to the DataTable
dt.Rows.Add(New String() { "Root", "0" })
dt.Rows.Add(New String() { "Work", "Root" })
dt.Rows.Add(New String() { "Home", "Root" })
dt.Rows.Add(New String() { "One", "Work" })
dt.Rows.Add(New String() { "Two", "Work" })
dt.Rows.Add(New String() { "One", "Home" })
dt.Rows.Add(New String() { "Two", "Home" })
dt.Rows.Add(New String() { "Three", "Home" })
dt.Rows.Add(New String() { "Other", "Root" })
'Add DataTable to the DataSet
ds.Tables.Add(dt)
'Add a relationship
ds.Relations.Add("TreeParentChild", ds.Tables("AddressLists").Columns("Name")
ds.Tables("AddressLists").Columns("Parent"), False)
''' <summary>
''' To Populate TreeView
''' </summary>
Public Sub PopulateTree(ByVal dr As DataRow, ByVal pNode As TreeNodeAdv)
'To iterate through all the rows in the DataSet
For Each row As DataRow In dr.GetChildRows("TreeParentChild")
'Creating a TreeNode for each row
Dim cChild As New TreeNodeAdv(row("Name").ToString())
'Add cChild node to the pNode
pNode.Nodes.Add(cChild)
'Recursively build the tree
PopulateTree(row, cChild)
Next row
End Sub
''' <summary>
''' Button Click event
''' </summary>
Private Sub buttonAdv1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonAdv1.Click
'Iterate all the rows in the DataSet
For Each dr As DataRow In ds.Tables("AddressLists").Rows
If dr("Parent").ToString() = "0" Then
'Creates a TreeNode if the parent equals 0
Dim root As New TreeNodeAdv(dr("Name").ToString())
treeViewAdv1.Nodes.Add(root)
'Recursively builds the tree
PopulateTree(dr, root)
End If
Next dr
'Expands all the tree nodes
treeViewAdv1.ExpandAll()
End Sub