请问你是如何实现c#winform访问access数据库,并用数据库动态生成treeview节点的功能的?
4个回答
展开全部
数据-添加数据源-数据库-数据集-新建连接-更改-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 = 0; i < source.Nodes.Count; i++)
{
desti.Nodes.Add((TreeNode)source.Nodes[i].Clone());
}
}
public void CloneTreeNode(TreeNode source, TreeNode desti) //复制两个节点
{
for (int i = 0; i < source.Nodes.Count; i++)
{
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);
}
}
}
至于动态生成树节点,需要写代码实现,没有简单的办法。我这儿有一个不知道你能不能看懂。
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 = 0; i < source.Nodes.Count; i++)
{
desti.Nodes.Add((TreeNode)source.Nodes[i].Clone());
}
}
public void CloneTreeNode(TreeNode source, TreeNode desti) //复制两个节点
{
for (int i = 0; i < source.Nodes.Count; i++)
{
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);
}
}
}
展开全部
先分两个步骤吧..
1.连接Access数据库,并从中获取数据..这个网上也很多.
2.动态绑定TreeView控件....动态绑定,网上很多..
有问题可以找我..
1.连接Access数据库,并从中获取数据..这个网上也很多.
2.动态绑定TreeView控件....动态绑定,网上很多..
有问题可以找我..
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public void Bind()
{
this.treeView1.Nodes.Clear();
AddTree(0, (TreeNode)null);
}
public void AddTree(int PID, TreeNode pNode)
{
DataTable dt = new DataTable();
dt = AccessFactory.getDataTable(" select * from Anotomical");
DataView dvTree = new DataView(dt);
foreach (DataRowView Row in dvTree)
{
TreeNode Node = new TreeNode();
Node.Text = Row["description"].ToString();
Node.Tag = Row["aid"].ToString();
BindSubTree(Convert.ToInt32(Row["aid"].ToString()), Node);
this.treeView1.Nodes.Add(Node);
}
}
private void BindSubTree(int Pid, TreeNode PNode)
{
DataTable subDt = new DataTable();
subDt = AccessFactory.getDataTable(string.Format("select * from subAnotomical where aid='{0}'", Pid));
// TreeNode subNode = null;
if (subDt.Rows.Count != 0 && PNode != null)
{
foreach (DataRow dr in subDt.Rows)
{
TreeNode subNode = new TreeNode();
subNode.Text = dr["description"].ToString();
subNode.Tag = dr["aid"].ToString();
PNode.Nodes.Add(subNode);
}
}
}
{
this.treeView1.Nodes.Clear();
AddTree(0, (TreeNode)null);
}
public void AddTree(int PID, TreeNode pNode)
{
DataTable dt = new DataTable();
dt = AccessFactory.getDataTable(" select * from Anotomical");
DataView dvTree = new DataView(dt);
foreach (DataRowView Row in dvTree)
{
TreeNode Node = new TreeNode();
Node.Text = Row["description"].ToString();
Node.Tag = Row["aid"].ToString();
BindSubTree(Convert.ToInt32(Row["aid"].ToString()), Node);
this.treeView1.Nodes.Add(Node);
}
}
private void BindSubTree(int Pid, TreeNode PNode)
{
DataTable subDt = new DataTable();
subDt = AccessFactory.getDataTable(string.Format("select * from subAnotomical where aid='{0}'", Pid));
// TreeNode subNode = null;
if (subDt.Rows.Count != 0 && PNode != null)
{
foreach (DataRow dr in subDt.Rows)
{
TreeNode subNode = new TreeNode();
subNode.Text = dr["description"].ToString();
subNode.Tag = dr["aid"].ToString();
PNode.Nodes.Add(subNode);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
百度一下,到处都是。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询