Dynamic loading tree
Chen Genfa
Proposition: Read the field value from the database as the node of the tree
achieve:
One. Put the field value to the DataSet
two. Add a root node
three. Add corresponding child nodes
code show as below
Using system;
Using system.drawing;
Using system.collections;
Using system.componentmodel;
Using system.windows.forms;
Using system.data;
Namespace WindowsApplication3
{
///
/// Form1 summary description.
/// summary>
Public Class Form1: System.Windows.Forms.form
{
Private system.windows.Forms.treeView TreeView1;
///
/// The required designer variable.
/// summary>
Private system.componentmodel.Container Components = NULL;
Public Form1 ()
{
//
// Windows Form Designer Support
//
InitializationComponent ();
//
// Todo: Add any constructor code after INITIALIZECOMPONENT call
//
}
///
/// Clean all the resources being used.
/// summary>
Protected Override Void Dispose (Bool Disposing)
{
IF (Disposing)
{
IF (Components! = NULL)
{
Components.dispose ();
}
}
Base.dispose (Disposing);
}
#Region Windows Form Designer Generated Code
///
/// Designer supports the required method - do not use the code editor to modify
/// This method is content.
/// summary>
Private vidinitiRizeComponent ()
{
This.treeView1 = new system.windows.forms.treeview ();
THIS.SUSPENDLAYOUT ();
//
// TreeView1
//
this.treeView1.dock = system.windows.forms.dockstyle.bottom;
THIS.TREEVIEW1.ImageIndex = -1;
This.treeView1.Location = new system.drawing.point (0, 137);
THIS.TREEVIEW1.NAME = "TreeView1";
This.treeView1.selectedImageIndex = -1;
This.treeView1.size = new system.drawing.size (352, 136);
THIS.TREEVIEW1.TABINDEX = 0;
This.treeView1.AfTerselect = new system.windows.forms.treeviewEventhandler (this.treeView1_aftelect);
//
// Form1
//
THIS.AUTOSCALEBASESIZE = New System.drawing.size (6, 14);
This.ClientSize = New System.drawing.size (352, 273);
This.Controls.add (this.treeView1);
THIS.NAME = "Form1";
THIS.TEXT = "Form1";
This.Load = New System.EventHandler (this.form1_load);
This.ResumeLayout (false);
}
#ndregion
///
/// The main entry point for the application.
/// summary>
[Stathread]
Static void
Main
()
{
Application.run (New Form1 ());
}
Private void TreeView1_AFTERSELECT (Object Sender, System.Windows.Forms E)
{
}
Private Void Form1_Load (Object Sender, System.EventArgs E)
{
Showform ();
}
Private void showform ()
{
TreeView1.nodes.clear ();
System.windows.Forms.treenode Mnode = New Treenode ("All");
//mnode.imageindex = 1;
TreeView1.nodes.add (mnode);
/ / Assume that DataSet has been obtained
System.windows.Forms.treenode nNode = New Treenode ("in-job");
System.windows.Forms.treenode Pnode = New Treenode ("Deputy");
TreeView1.topnode.nodes.add (nnode);
TreeView1.topnode.Nodes.Add (PNODE);
TreeView1.select ();
IF (ds.tables [0] .Rows.count <= 0)
{
Return;
}
Else
{
// Add add some childtreenodes
Foreach (DataRow Dr in Ds.Tables [0] .ROWS)
{
System.windows.Forms.treenode Anode = New Treenode (DR ["Name"]. TOSTRING ());
System.windows.Forms.treenode Bnode = New Treenode (DR ["Name"]. TOSTRING ());
TreeView1.nodes [0] .Nodes [0] .NODES .ADD (anode);
TreeView1.nodes [0] .nodes [1] .NODES .ADD (BNODE);
}
}
}
}
}
Ok, the value of the node is the field value!