treeview绑定怎么用TreeView绑定数据库里的省市县数据

treeview绑定  时间:2021-06-04  阅读:()

C#中TreeView与数据库绑定?

递归方法。

// /// TreeView数据绑定 /// /// protected void CreateTreeView() { TreeNode rootNode = new TreeNode(); rootNode.Text = "全部"; rootNode.Value = "-1"; BasicTypeTreeView.Nodes.Add(rootNode); BasicInformationManager BasicManager = new BasicInformationManager(); var tlist = BasicManager.GetAllBasicInfoTypes(); foreach (Model.BasicInforType mtype in tlist) { if (mtype.ParentID == 0) { TreeNode Node = new TreeNode(); //添加根节点 Node.Text = mtype.BasicTypeName; Node.Value = mtype.ID.ToString(); Node.ToolTip = "编码" + mtype.BasicCode; getSubNode(ref Node, mtype.ID); rootNode.ChildNodes.Add(Node); } } } /// /// 获取treeView子节点 /// /// /// public void getSubNode(ref TreeNode node, int item) { BasicInformationManager BasicManager = new BasicInformationManager(); var tlist = BasicManager.GetBasicInforTypeByParentId(item); if (tlist.Count > 0) { foreach (Model.BasicInforType m in tlist) { TreeNode subNode = new TreeNode(); subNode.Text = m.BasicTypeName; subNode.Value = m.ID.ToString(); subNode.ToolTip = "编码" + m.BasicCode; getSubNode(ref subNode, m.ID); node.ChildNodes.Add(subNode); //进行子节点循环添加 } } }

C#手动绑定treeview怎么写来着?

private void addTree(TreeNode tn, string dept) { DataTable dt = cf.getDataTable("select 部门名称 from 部门表 where 上级部门= + dept + "); foreach (DataRow dr in dt.Rows) { TreeNode ctn = new TreeNode(dr[0].ToString()); ctn.SelectedImageIndex = 0; ctn.ImageIndex = 0; DataTable dtg = cf.getDataTable("select 岗位名称 from 岗位表 where 所属部门= + dr[0].ToString() + "); foreach (DataRow drg in dtg.Rows) { TreeNode tng = new TreeNode(drg[0].ToString()); tng.SelectedImageIndex = 1; tng.ImageIndex = 1; ctn.Nodes.Add(tng); } tn.Nodes.Add(ctn); addTree(ctn,dr[0].ToString()); } }

private void FormManagerCenter_Load(object sender, EventArgs e) { TreeNode tn = new TreeNode("公司架构"); addTree(tn, "公司架构"); treeView1.Nodes.Add(tn); }

利用递归原理

WinForm中数据绑定TreeView

这是一个绑定的方法,可以参考一下! public void InitTvArea() { Area.Nodes.Clear(); TreeNode rootNode =Area.Nodes.Add("区域划分"); IList<Area> listArea = AreaManager.Get_Area_Father(); foreach (Area area in listArea) { TreeNode node = new TreeNode(area.AreaName); //根节点 Area areaid = AreaManager.Get_Area_idByName(area.AreaName); IList<Area> listAreaChild = AreaManager.Get_Area_Childs(areaid.Id + "___"); if (listAreaChild.Count != 0) { foreach (Area areaChild in listAreaChild) { TreeNode childNode = new TreeNode(areaChild.AreaName); //子节点 node.Nodes.Add(childNode); } } rootNode.Nodes.Add(node); //添加字节点 } rootNode.Expand(); }

怎么用TreeView绑定数据库里的省市县数据

数据表: CREATE TABLE CateTable ( [ID] [int] IDENTITY (1, 1) NOT NULL , [CateName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL , [RootID] [int] NOT NULL , [ParentID] [int] NOT NULL ) GO 存储过程: CREATE PROCEDURE CateTable_GetList AS BEGIN Select CateID, CateName, RootID, ParentID From CateTable Order By CateID,RootID,ParentID END 代码: private void MainForm_Load(object sender, EventArgs e) { //从数据库中读取数据 SqlConnection con = new SqlConnection(ConnString.ConStr); SqlCommand cmd = new SqlCommand("CateTable_GetList", con); cmd.CommandType = CommandType.StoredProcedure; SqlDataAdapter sda = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); try { sda.Fill(ds); } catch { } finally { cmd = null; con.Close(); } //往TreeView中添加树节点 //添加根节点 TreeNode tn = new TreeNode(); tn.Text = "种类"; tn.Name = "0";//Name作为ID tn.Tag = "0";//Tag作为RootID tn.ImageIndex = 0; tn.SelectedImageIndex = 0; tv.Nodes.Add(tn);//该TreeView命名 tv.SelectedNode =.TopNode; //把其他节点加上去 if (ds != null) { foreach (DataRow dr in ds.Tables[0].Rows) { tn = new TreeNode(); tn.Text = dr["CateName"].ToString(); tn.Name = dr["CateID"].ToString();//Name作为CateID tn.Tag = dr["RootID"].ToString();//Tag作为RootID tn.ImageIndex = 1; tn.SelectedImageIndex = 1; //判断是否为主节点 if (dr["CateID"].ToString() == dr["RootID"].ToString()) { //主节点 tv.SelectedNode =.TopNode; } else { //其他节点 if .SelectedNode.Name != dr["ParentID"].ToString()) { TreeNode[] tn_temp =.Nodes.Find(dr["ParentID"].ToString(), true); if (tn_temp.Length > 0) { tv.SelectedNode = tn_temp[0]; } } } tv.SelectedNode.Nodes.Add(tn); } tv.ExpandAll();//展开TreeView tv.SelectedNode =.TopNode; } }

创梦网络-四川一手资源高防大带宽云服务器,物理机租用,机柜资源,自建防火墙,雅安最高单机700G防护,四川联通1G大带宽8.3W/年,无视UDP攻击,免费防CC

? ? ? ?创梦网络怎么样,创梦网络公司位于四川省达州市,属于四川本地企业,资质齐全,IDC/ISP均有,从创梦网络这边租的服务器均可以****,属于一手资源,高防机柜、大带宽、高防IP业务,另外创梦网络近期还会上线四川联通大带宽,四川联通高防IP,一手整CIP段,四川电信,联通高防机柜,CN2专线相关业务。成都优化线路,机柜租用、服务器云服务器租用,适合建站做游戏,不须要在套CDN,全国访问快...

安徽BGP云服务器 1核 1G 5M 29元/月 香港云服务器 1核 1G 19元首月 麻花云

麻花云怎么样?麻花云公司成立于2007年,当前主打产品为安徽移动BGP线路,数据中心连入移动骨干网。提供5M,10M大带宽云主机,香港云服务器产品,数据中心为香港将军澳机房,香港宽频机房 cn2-GIA优质线路、采用HYPER-V,KVM虚拟技术架构一、麻花云官网点击直达麻花云官方网站合肥网联网络科技有限公司优惠码: 专属优惠码:F1B07B 享受85折优惠。最新活动 :双11 云上嗨购 香港云主...

ShockHosting($4.99/月),东京机房 可享受五折优惠,下单赠送10美金

ShockHosting商家在前面文章中有介绍过几次。ShockHosting商家成立于2013年的美国主机商,目前主要提供虚拟主机、VPS主机、独立服务器和域名注册等综合IDC业务,现有美国洛杉矶、新泽西、芝加哥、达拉斯、荷兰阿姆斯特丹、英国和澳大利亚悉尼七大数据中心。这次有新增日本东京机房。而且同时有推出5折优惠促销,而且即刻使用支付宝下单的话还可获赠10美金的账户信用额度,折扣相比之前的常规...

treeview绑定为你推荐
donghang东航电子登机牌如何使用请求超时DNS请求超时,怎么办?匹配函数excel中vlookup函数的用法awv请问awv是什么样的格式啊?印度尼西亚国家代码谁知道世界各国的国家电话代码?jstz举手望,草上马跑,打什么数字?数据挖掘项目什么是数据挖掘?从事相关的工作有什么要求?腾讯技术腾讯简介----assemblyinfoLOL的 X、L、CS 是什么意思单元测试规范如何做好小学数学单元测试工作
广州服务器租用 美国加州vps singlehop siteground 12306抢票助手 域名接入 域名和空间 网通服务器托管 浙江服务器 江苏徐州移动 宿迁服务器 阿里云邮箱申请 杭州电信宽带 香港博客 架设代理服务器 webmin 卡巴斯基免费下载 电脑主机 什么是云主机 新浪轻博客 更多