TreeView数据绑定的方法(1)
TreeView数据绑定的方法(1)
最近作一个.net程序中涉及到权限的分配和管理,要把分为不同层次的权限从数据库中读出在树形控件TreeView中显示出来。这里采取的是首先取出数据放在DataTable中,然后在递归查询中根据不同的filter条件得到对应树杈需要的数据。这里使用类似的行政区划的数据表,数据表为:
CREATE TABLE [dbo].[QuHua] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,--自增的标记列
[NAME] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,--行政区划的名称
[ParentID] [int] NOT NULL --行政区划的上一级区划ID,最高级区划的上一级默认为0
) ON [PRIMARY]
GO
---插入测试数据
Insert Into dbo.QuHua (NAME,ParentID) Values ('北京',0)
Insert Into dbo.QuHua (NAME,ParentID) Values ('山东',0)
Insert Into dbo.QuHua (NAME,ParentID) Values ('河北',0)
Insert Into dbo.QuHua (NAME,ParentID) Values ('海淀',1)
Insert Into dbo.QuHua (NAME,ParentID) Values ('中关村',4)
Insert Into dbo.QuHua (NAME,ParentID) Values ('济宁',2)
Insert Into dbo.QuHua (NAME,ParentID) Values ('曲阜',6)
Insert Into dbo.QuHua (NAME,ParentID) Values ('济南',2)
/// <summary>
/// 根据递归查询结果动态生成TreeView
/// 由于算法效率问题,不适合数据量的情况
/// </summary>
private void CreateTreeView()
{
string connetion = "Data Source=. ;Initial Catalog=MIS_New;Integrated Security=True";
using (SqlConnection cn = new SqlConnection(connetion))
{
cn.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from QuHua", cn);
DataTable dt = new DataTable();
da.Fill(dt);
//首先把第一级的行政区划取出生成TreeView的节点
//作为递归运算的入口
CreateTreeViewRecursive(treeView1.Nodes, dt, 0);
}
![](/Images/OutliningIndicators/InBlock.gif)
}
/// <summary>
/// 递归查询
/// </summary>
/// <param name="nodes">TreeView的节点集合</param>
/// <param name="dataSource">数据源</param>
/// <param name="parentid">上一级行政区划的标识码</param>
private void CreateTreeViewRecursive(TreeNodeCollection nodes,DataTable dataSource,int parentid)
{
string filter;
filter = string.Format("parentid={0}", parentid);
DataRow[] drarr = dataSource.Select(filter);
TreeNode node;
foreach (DataRow dr in drarr)
{
node = new TreeNode();
node.Text = (string)dr["name"];
node.Tag = (int)dr["id"];
nodes.Add(node);
CreateTreeViewRecursive(node.Nodes,dataSource,(int)node.Tag);
}
![](/Images/OutliningIndicators/InBlock.gif)
}
CREATE TABLE [dbo].[QuHua] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,--自增的标记列
[NAME] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,--行政区划的名称
[ParentID] [int] NOT NULL --行政区划的上一级区划ID,最高级区划的上一级默认为0
) ON [PRIMARY]
GO
---插入测试数据
Insert Into dbo.QuHua (NAME,ParentID) Values ('北京',0)
Insert Into dbo.QuHua (NAME,ParentID) Values ('山东',0)
Insert Into dbo.QuHua (NAME,ParentID) Values ('河北',0)
Insert Into dbo.QuHua (NAME,ParentID) Values ('海淀',1)
Insert Into dbo.QuHua (NAME,ParentID) Values ('中关村',4)
Insert Into dbo.QuHua (NAME,ParentID) Values ('济宁',2)
Insert Into dbo.QuHua (NAME,ParentID) Values ('曲阜',6)
Insert Into dbo.QuHua (NAME,ParentID) Values ('济南',2)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
(注:记录一下这些内容只是以后查阅起来方便,作为自己知识积累的记录。其中有很多是参考网络上的资源,不再一一写出出处,还请原作者见谅。)