C#动态生成treeview方法
我的数据表字段如下
ID name parentID
相信我一写这个大家都只到我要问的问题了 我就不多说了 就是C#通过调用此表中有级联关系的数据 遍历的生成一个多层动态树形目录,最好能注释一下说明 我们用过 所以不大好理解 ,请教大虾们 小弟感激不尽...
------解决方案-------------------- '根节点查询
Public Function RootNode() As DataRow()
Dim da As New DataAccessor
Dim dt As New DataTable
Dim ds As New DataSet
Dim dr As DataRelation
Dim Root As DataRow()
Try
da = DataAccessorFactory.GetDataAccessor("Xl_Zy_Tree.select")
ds = da.ExecuteQueryAsDataSet(da.PreparedSql)
dt = ds.Tables(0)
dr = New DataRelation("SD_JX_ZUOYELEIXING", dt.Columns("ZUOYELEIXINGBIANHAO"), dt.Columns("FUJIEDIANBIANHAO"))
dt.DataSet.Relations.Add(dr)
Root = dt.Select("FUJIEDIANBIANHAO = '001'")
Return Root
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Function
'FORM 窗体进行调用,使用的是递归
Private Function BulidTree()
Dim root As DataRow()
Dim logic As New LogicZuoYeLeiXingWeiHu
Dim model As New ZuoYeLeiXing
Dim node As TreeNode
Dim dr As DataRow
Dim child As DataRow()
root = logic.RootNode()
'递归添加根节点
For Each dr In root
node = New TreeNode
node.Text = CType(dr("ZUOYELEIXINGMINGCHENG"), String)
node.Tag = CType(dr("ZUOYELEIXINGBIANHAO"), String)
Me.tree_View.Nodes.Add(node)
child = dr.GetChildRows("SD_JX_ZUOYELEIXING")
If child.Length = 0 Then
Else
BulidTree(node, child)
End If
Next
End Function
Private Function BulidTree(ByVal tn As TreeNode, ByVal row As DataRow())
Dim newNode As TreeNode
Dim dr As DataRow
Dim child As DataRow()
Try
'递归添加子节点
For Each dr In row
newNode = New TreeNode
newNode.Text = CType(dr("ZUOYELEIXINGMINGCHENG"), String)
newNode.Tag = CType(dr("ZUOYELEIXINGBIANHAO"), String)
tn.Nodes.Add(newNode)
child = dr.GetChildRows("SD_JX_ZUOYELEIXING")
If child.Length = 0 Then
Else
BulidTree(newNode, child)
End If
Next
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Function
一直使用的一个方法,希望能帮助楼主
------解决方案--------------------哈哈 我刚好也在搞这个东西 这是一段小代码 是只动态绑定数据库中的表名 通过表名点击显示表
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetTree();
}
}
public void GetTree() // 这段是通过循环把表名添加到树的节点上 获取表名是select name from sysobjects WHERE xtype = 'U'
{
db.open(); //andstatus>=0 这段是数据库中所有表的表名
sqladap = new SqlDataAdapter(db.selectTable, db.sqlcon);
sqladap.Fill(dt);
for (int i = 0; i < dt.Rows.Count-5; i++)
{
TreeNode tn = new TreeNode();
tn.Text = dt.Rows[i]["name"].ToString();
TreeView1.Nodes.Add(tn);
}
}
public void bind() //这段是绑定 通过点击把表显示在GRIDVIEW上
{
string str = this.TreeView1.SelectedNode.Text;
db.open();
sqladap = new SqlDataAdapter("select * from &quo