1、数据库设计
// CREATE TABLE [SY_Department] (
// [DepID] [varchar] (8) COLLATE Chinese_PRC_CI_AS NOT NULL ,
// [DepName] [varchar] (30) COLLATE Chinese_PRC_CI_AS NOT NULL ,
// [ParentID] [varchar] (8) COLLATE Chinese_PRC_CI_AS NULL ,
// [Memo] [varchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
// CONSTRAINT [PK_SY_Department] PRIMARY KEY CLUSTERED
// (
// [DepID]
// ) ON [PRIMARY]
// ) ON [PRIMARY]
// GO
//
//
2、代码说明
private void BindDep()
{
SysCommon.AccessBase.SqlHelp sp=new SysCommon.AccessBase.SqlHelp();
DataSet ds = sp.GetDataSet("select depid,depname,parentid,memo from sy_department");
DataTable dt = ds.Tables[0];
if(dt.Rows.Count<=0) return ;
treeView1.Nodes.Clear();
//插入根节点
DataRow[] drArr = dt.Select("depid='0'", "depid asc");
System.Windows.Forms.TreeNode nd1=new TreeNode();
nd1.Tag=drArr[0]["depid"].ToString();
nd1.Text=drArr[0]["depname"].ToString();
treeView1.Nodes.Add(nd1);
//插入子节点
CreateTreeNode(ref nd1, dt);
treeView1.ExpandAll();
treeView1.CheckBoxes=true;
}
private void CreateTreeNode(ref TreeNode tnParent, DataTable dt)
{
DataRow[] drArr = dt.Select("parentid = '" + tnParent.Tag + "'", "depid asc");
if (drArr.Length == 0) return;
for (int i = 0; i < drArr.Length; i++)
{
DataRow row = drArr[i];
System.Windows.Forms.TreeNode nd=new TreeNode();
nd.Tag=row["depid"].ToString();
nd.Text=row["depname"].ToString();
tnParent.Nodes.Add(nd);
CreateTreeNode(ref nd, dt);
}
}
//选中节点或取消选中节点
private void CheckChilds(TreeNode tnParent)
{
foreach(TreeNode nd in tnParent.Nodes)
{
nd.Checked=tnParent.Checked;
//CheckChilds(nd);
}
}
private void treeView1_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
TreeNode pnd=e.Node;
CheckChilds(pnd);
}
//遍历所有节点得到选中的节点放入ArrayList中
private void GetCheckNodes(TreeNode pnd,ref ArrayList list)
{
foreach(TreeNode nd in pnd.Nodes)
{
if(nd.Checked)
{
list.Add(nd.Tag.ToString());
}
GetCheckNodes(nd,ref list);
}
}