为什么能遍历树根,子根不出来,数据库设计结构、运行结果及代码如下 :
数据库设计结构:
运行结果:
代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace tree
{
public partial class Form1 : Form
{
private DataTable myTable = null;
private TreeNode tn;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=WWW-6C6BB276467;database=test1;uid=sa;pwd=123456;");
SqlDataAdapter myDa = new SqlDataAdapter("select * from TreeList", conn);
DataSet myDs = new DataSet();
myDa.Fill(myDs, "Table");
DataTable myTable = myDs.Tables[0];
//加载树
//获得所有功能模块数据
//为筛选创建视图
DataView myDv = myTable.DefaultView;
//筛选出根节点相关信息
myDv.RowFilter = "funcparent = 'null'";
//创建一个TreeNode ,并且将该节点加入到树中
//使其成为根节点。
for (int i = 0; i < myDv.Count; i++)
{
tn = new TreeNode(myDv[i][1].ToString());
treeView1.Nodes.Add(tn);
BianLiwrte(tn);
}
}
private void BianLiwrte(TreeNode tn)
{
DataView myDv = new DataView(myTable);
myDv.RowFilter = "funcparent='" + tn.Text + "'";
for (int i = 0; i < myDv.Count; i++)
{
TreeNode tnLcoal = new TreeNode(myDv [i][1].ToString ());
tn.Nodes.Add(tnLcoal);
if (myDv[i][3].ToString() == "True")
{
BianLiwrte(tnLcoal);
}
}
}
}
}