using System.Windows.Forms; namespace Test { class HowEasy:Form { private System.Windows.Forms.TreeView treeView1;
/// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any 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 /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.treeView1 = new System.Windows.Forms.TreeView();
this.SuspendLayout(); // // treeView1 // this.treeView1.ImageIndex = -1; this.treeView1.Location = new System.Drawing.Point(8, 8); this.treeView1.Name = "treeView1"; this.treeView1.SelectedImageIndex = -1; this.treeView1.Size = new System.Drawing.Size(264, 360); this.treeView1.TabIndex = 0; //treeView1上添加节点 this.treeView1.Cnotrols.AddRange(new TreeNode[]{ new TreeNode("FirstLevel"),new TreeNode("SceondLevel") }); //HowEasy this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(616, 381); this.Controls.Add(this.treeView1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new Form1());
namespace Test
{
class HowEasy:Form
{
private System.Windows.Forms.TreeView treeView1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
// TODO: Add any constructor code after InitializeComponent call
//
} /// <summary>
/// Clean up any 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
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.treeView1 = new System.Windows.Forms.TreeView();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.ImageIndex = -1;
this.treeView1.Location = new System.Drawing.Point(8, 8);
this.treeView1.Name = "treeView1";
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(264, 360);
this.treeView1.TabIndex = 0;
//treeView1上添加节点
this.treeView1.Cnotrols.AddRange(new TreeNode[]{ new TreeNode("FirstLevel"),new TreeNode("SceondLevel") });
//HowEasy
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(616, 381);
this.Controls.Add(this.treeView1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
#endregion /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}
TreeView空件主要常用的是使用递归算法进行显示如:
假设有数据源DataTable dt,其中已有数据,结构为ID,PID,Name...
DataRow dr = GetRootDr(dt);//取得根记录
TreeView tv = new Treeview();
TreeNode tnParent = tv.Nodes[0];
tnParent.Text = dr["Name"].ToString();
tbParent.Tag = dr["ID"].ToString();
tv.Nodes.Add(tnParent);
DispChild(tnParent,dt) private void DispChild(TreeNode tnParent,DataTable dt)
{
DataRow[] drs = GetChildren(tnParent.Text,dt);//获取下级节点信息
foreach(DataRow dr in drs)
{
TreeNode tn = tv.Nodes[0];
tn.Text = dr["Name"].ToString();
tn.Tag = dr["ID"].ToString();
tnParent.Nodes.Add(tn);
DispChild(tnParent,dt) }
}
可以传给你控件和一些代码。
[email protected]