c#怎么从数据库读表并动态生成treeview

c#怎么从数据库读表并动态生成treeview,第1张

呵呵,这个依次取出数据,一一声明treenode赋值即可!

可以考虑:

1.先整体取出LName的数据,

2。再循环遍历LName数据,每一个LName为其声明一个treenode对象,且根据当前Lname获取子数据CName,再内嵌一个循环遍历子数据CName,每一个CName对应声明一个treenode对象(这些treenode作为当前LName对应treenode的子节点)。

数据-添加数据源-数据库-数据集-新建连接-更改-Access数据库文件-一直下去就能访问access数据库,也能产生默认的链接串。

至于动态生成树节点,需要写代码实现,没有简单的办法。我这儿有一个不知道你能不能看懂。

using System

using System.Collections.Generic

using System.Text

using System.Windows.Forms

using System.Data

using System.Data.SqlClient

namespace LanMsg.Common

{

public class ClassTreeView

{

private TreeView tv

public ClassTreeView()

{

}

public ClassTreeView(TreeView TV)

{

tv = TV

}

public int TreeType(TreeNode tn)//判断节点类型

{

switch (tn.ImageIndex)

{

case 0:

case 1:

return 1

case 2:

case 3:

case 4:

case 5:

case 6:

case 7:

case 19:

return 2

}

return 0

}

public TreeNode FindNode(TreeNode tnParent, int DeptID)

{

if (tnParent == null)

return null

if (TreeType(tnParent) == 1)

{

ClassDeptment tag = (ClassDeptment)tnParent.Tag

if (tag.DeptID == DeptID) return tnParent

}

TreeNode tnRet = null

foreach (TreeNode tn in tnParent.Nodes)

{

tnRet = FindNode(tn, DeptID)

if (tnRet != null) break

}

return tnRet

}

public TreeNode FindNode(TreeNode tnParent, string UserID)

{

if (tnParent == null)

return null

if (TreeType(tnParent) == 2)

{

ClassUserInfo tag = (ClassUserInfo)tnParent.Tag

if (String.Equals(tag.UserID, UserID)) return tnParent

}

TreeNode tnRet = null

foreach (TreeNode tn in tnParent.Nodes)

{

tnRet = FindNode(tn, UserID)

if (tnRet != null) break

}

return tnRet

}

public void CloneTreeView(TreeView source, TreeView desti) //复制两棵树

{

for (int i = 0i <source.Nodes.Counti++)

{

desti.Nodes.Add((TreeNode)source.Nodes[i].Clone())

}

}

public void CloneTreeNode(TreeNode source, TreeNode desti) //复制两个节点

{

for (int i = 0i <source.Nodes.Counti++)

{

desti.Nodes.Add((TreeNode)source.Nodes[i].Clone())

}

}

public void AddTree(TreeNode pNode, DataSet ds)//添加树的节点

{

if (pNode == null)

{ //根节点

DataView dvTree = new DataView(ds.Tables["dept"])

dvTree.RowFilter = "[PARENTID] = 1"

foreach (DataRowView Row in dvTree)

{

TreeNode Node = new TreeNode()

ClassDeptment dept = new ClassDeptment()

Node.ImageIndex = 0 //如果是单位,选择图像1

Node.SelectedImageIndex = 1

dept.DeptID = Int32.Parse(Row["id"].ToString())

dept.Deptment = Row["name"].ToString()

Node.Tag = dept

Node.Text = dept.Deptment

tv.Nodes.Add(Node)

}

}

else

{

ClassDeptment dt = (ClassDeptment)pNode.Tag

DataView dvTree = new DataView(ds.Tables["dept"])

dvTree.RowFilter = "[PARENTID] = " + dt.DeptID

foreach (DataRowView Row in dvTree)

{

TreeNode Node = new TreeNode()

ClassDeptment dept = new ClassDeptment()

Node.ImageIndex = 0 //如果是单位,选择图像1

Node.SelectedImageIndex = 1

dept.DeptID = Int32.Parse(Row["id"].ToString())

dept.Deptment = Row["name"].ToString()

Node.Tag = dept

Node.Text = dept.Deptment

pNode.Nodes.Add(Node)

}

dvTree = new DataView(ds.Tables["user"])

dvTree.RowFilter = "[deptID] = " + dt.DeptID

foreach (DataRowView Row in dvTree)

{

TreeNode Node = new TreeNode()

ClassUserInfo user = new ClassUserInfo()

if (Int32.Parse(Row["state"].ToString()) == 0)

{

Node.ImageIndex = 19

}

else

{

Node.ImageIndex = 2

}

Node.SelectedImageIndex = 3

user.UserID = Row["Staff_Name"].ToString()

user.UserName = Row["RealName"].ToString()

Node.Tag = user

Node.Text = user.UserName

pNode.Nodes.Add(Node)

}

}

}

public void AddTree(int ParentID, TreeNode pNode, DataSet ds)

{

DataView dvTree = new DataView(ds.Tables["dept"])

DataView duTree = new DataView(ds.Tables["user"])

//过滤ParentID,得到当前的所有子节点

if (pNode == null)

dvTree.RowFilter = "[ID] = 1"

else

dvTree.RowFilter = "[ID] <>1 and [PARENTID] = " + ParentID

foreach (DataRowView Row in dvTree)

{

TreeNode Node = new TreeNode()

ClassDeptment dept = new ClassDeptment()

if (pNode == null)

{ //添加根节点

//记录单位的ID和单位数据表

dept.DeptID = Convert.ToInt32(Row["id"])

dept.Deptment = Row["name"].ToString()

Node.Tag = dept

Node.Text = dept.Deptment

Node.ImageIndex = 0

Node.SelectedImageIndex = 1

tv.Nodes.Add(Node)

AddTree(dept.DeptID, Node, ds) //再次递归

}

else

{ //添加当前节点的子节点

//记录单位的ID和单位数据表

dept.DeptID = Convert.ToInt32(Row["id"])

dept.Deptment = Row["name"].ToString()

Node.Tag = dept

Node.Text = dept.Deptment

Node.ImageIndex = 0

Node.SelectedImageIndex = 1

pNode.Nodes.Add(Node)

tv.SelectedNode = Node

duTree.RowFilter = "[deptID] = " + Int32.Parse(Row["id"].ToString()) //增加部门人员

int i = 0

foreach (DataRowView uRow in duTree)

{

TreeNode uNode = new TreeNode()

ClassUserInfo user = new ClassUserInfo()

//记录人员的ID和人员数据表

user.UserID = uRow["Staff_Name"].ToString()

user.UserName = uRow["RealName"].ToString()

uNode.Tag = user

uNode.Text = user.UserName

uNode.ImageIndex = 2

uNode.SelectedImageIndex = 3

if (i == 0)

{

AddChildNode(uNode)

i++

}

else

{

AddParent(uNode)

}

}

AddTree(dept.DeptID, Node, ds) //再次递归

}

}

}

public void AddChildNode(TreeNode tmp) //添加子节点

{

//首先判断是否选定组件中的位置

if (tv.SelectedNode == null)

{

MessageBox.Show("请选择一个节点", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information)

}

else

{

if (tmp.Text != "")

{

//在TreeView组件中加入子节点

tv.SelectedNode.Nodes.Add(tmp)

tv.SelectedNode = tmp

tv.ExpandAll()

}

else

{

MessageBox.Show("必须有节点名称!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information)

return

}

}

}

private void AddParent(TreeNode tmp)//添加父节点

{

//首先判断是否选定组件中节点的位置

if (tv.SelectedNode == null)

{

MessageBox.Show("请选择一个节点", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information)

}

else

{

if (tmp.Text != "")

{

//在TreeView组件中加入兄弟节点

tv.SelectedNode.Parent.Nodes.Add(tmp)

tv.ExpandAll()

}

else

{

MessageBox.Show("必须有节点名称!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information)

return

}

}

TreeNode tnode = new TreeNode(tmp.Text)

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6764257.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-28
下一篇 2023-03-28

发表评论

登录后才能评论

评论列表(0条)

保存