TreeView 是一个很好的表达树形结构的控件。如果要加载的数据比较多的话,就需要逐层加载数据。要实现逐层加载数据,首先想到的就是TreeView 的 BeforeExpand 事件,它在节点展开前触发。但问题是BeforeExpand 事件每次展开时都会触发。有没有只在首次展开才触发的事件呢,可惜没找到。没关系,我们自定义一个 TreeView 节点类,给它加一个是否首次被展开的属性。
结果:
/// <summary>
/// 自定义节点类,继承于系统的 TreeNode 类,给节点增加一个 IsFirstExpand 属性
/// </summary>
public class InheritTreeNode : TreeNode
{
private bool isFirstExpand = true;
public bool IsFirstExpand //属性
{
get { return isFirstExpand; }
set { isFirstExpand = value; }
}
public InheritTreeNode() : base() { }
public InheritTreeNode(string text) : base(text) { }
}
下面是一个加载省市的完整代码:/// 自定义节点类,继承于系统的 TreeNode 类,给节点增加一个 IsFirstExpand 属性
/// </summary>
public class InheritTreeNode : TreeNode
{
private bool isFirstExpand = true;
public bool IsFirstExpand //属性
{
get { return isFirstExpand; }
set { isFirstExpand = value; }
}
public InheritTreeNode() : base() { }
public InheritTreeNode(string text) : base(text) { }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace test
{
public partial class TreeStruc : Form
{
public TreeStruc()
{
InitializeComponent();
}
SqlConnection conn = new SqlConnection("server=zhuzy;integrated security=sspi;database=library");
SqlDataAdapter da;
DataSet ds=new DataSet();
/// <summary>
/// 自定义节点类,继承于系统的 TreeNode 类,给节点增加一个 IsFirstExpand 属性
/// </summary>
public class InheritTreeNode : TreeNode
{
private bool isFirstExpand = true;
public bool IsFirstExpand //属性
{
get { return isFirstExpand; }
set { isFirstExpand = value; }
}
public InheritTreeNode() : base() { }
public InheritTreeNode(string text) : base(text) { }
}
string id = "";
private void Tree_Load(object sender, EventArgs e)
{
da = new SqlDataAdapter("select province_id,province_name from province", conn);
da.Fill(ds, "province");
for (int i = 0; i < ds.Tables["province"].Rows.Count; i++)
{
InheritTreeNode root = new InheritTreeNode(ds.Tables["province"].Rows[i][1].ToString().Trim());
id = ds.Tables["province"].Rows[i][0].ToString().Trim();
this.treeView1.Nodes.Add(root);
AddChileNode(root);
id = "";//
}
}
/// <summary>
/// 添加 node 的子节点
/// </summary>
private void AddChileNode(TreeNode node)
{
da = new SqlDataAdapter("select city_name from city where province_id='" + id + "'", conn);
da.Fill(ds, "city");
try
{
for (int j = 0; j < ds.Tables["city"].Rows.Count; j++)
{
node.Nodes.Add(new InheritTreeNode(ds.Tables["city"].Rows[j][0].ToString().Trim()));
}
}
catch { }
ds.Tables["city"].Clear();//
}
/// <summary>
/// 节点展开之前事件
/// </summary>
private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
InheritTreeNode node = (InheritTreeNode)e.Node;//获取要选中、展开、折叠或选择的树节点e.Node
if (node != null)
{
// 判断该节点是否首次被展开
if (node.IsFirstExpand)
{
for (int i = 0; i < node.Nodes.Count; i++)
{
AddChileNode(node.Nodes[i]); // // 为 e.Node 下的每个子节点,添加子节点
}
}
}
node.IsFirstExpand = false;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace test
{
public partial class TreeStruc : Form
{
public TreeStruc()
{
InitializeComponent();
}
SqlConnection conn = new SqlConnection("server=zhuzy;integrated security=sspi;database=library");
SqlDataAdapter da;
DataSet ds=new DataSet();
/// <summary>
/// 自定义节点类,继承于系统的 TreeNode 类,给节点增加一个 IsFirstExpand 属性
/// </summary>
public class InheritTreeNode : TreeNode
{
private bool isFirstExpand = true;
public bool IsFirstExpand //属性
{
get { return isFirstExpand; }
set { isFirstExpand = value; }
}
public InheritTreeNode() : base() { }
public InheritTreeNode(string text) : base(text) { }
}
string id = "";
private void Tree_Load(object sender, EventArgs e)
{
da = new SqlDataAdapter("select province_id,province_name from province", conn);
da.Fill(ds, "province");
for (int i = 0; i < ds.Tables["province"].Rows.Count; i++)
{
InheritTreeNode root = new InheritTreeNode(ds.Tables["province"].Rows[i][1].ToString().Trim());
id = ds.Tables["province"].Rows[i][0].ToString().Trim();
this.treeView1.Nodes.Add(root);
AddChileNode(root);
id = "";//
}
}
/// <summary>
/// 添加 node 的子节点
/// </summary>
private void AddChileNode(TreeNode node)
{
da = new SqlDataAdapter("select city_name from city where province_id='" + id + "'", conn);
da.Fill(ds, "city");
try
{
for (int j = 0; j < ds.Tables["city"].Rows.Count; j++)
{
node.Nodes.Add(new InheritTreeNode(ds.Tables["city"].Rows[j][0].ToString().Trim()));
}
}
catch { }
ds.Tables["city"].Clear();//
}
/// <summary>
/// 节点展开之前事件
/// </summary>
private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
InheritTreeNode node = (InheritTreeNode)e.Node;//获取要选中、展开、折叠或选择的树节点e.Node
if (node != null)
{
// 判断该节点是否首次被展开
if (node.IsFirstExpand)
{
for (int i = 0; i < node.Nodes.Count; i++)
{
AddChileNode(node.Nodes[i]); // // 为 e.Node 下的每个子节点,添加子节点
}
}
}
node.IsFirstExpand = false;
}
}
}
结果: