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; } }

HostKvm($4.25/月),俄罗斯CN2带宽大升级,俄罗斯/香港高防限量5折优惠进行中

HostKvm是一家成立于2013年的国外VPS服务商,产品基于KVM架构,数据中心包括日本、新加坡、韩国、美国、俄罗斯、中国香港等多个地区机房,均为国内直连或优化线路,延迟较低,适合建站或者远程办公等。本月,商家旗下俄罗斯、新加坡、美国、香港等节点带宽进行了大幅度升级,俄罗斯机房国内电信/联通直连,CN2线路,150Mbps(原来30Mbps)带宽起,目前俄罗斯和香港高防节点5折骨折码继续优惠中...

npidc:9元/月,cn2线路(不限流量)云服务器,金盾+天机+傲盾防御CC攻击,美国/香港/韩国

npidc全称No Problem Network Co.,Limited(冇問題(香港)科技有限公司,今年4月注册的)正在搞云服务器和独立服务器促销,数据中心有香港、美国、韩国,走CN2+BGP线路无视高峰堵塞,而且不限制流量,支持自定义内存、CPU、硬盘、带宽等,采用金盾+天机+傲盾防御系统拦截CC攻击,非常适合建站等用途。活动链接:https://www.npidc.com/act.html...

个人网站备案流程及注意事项(内容方向和适用主机商)

如今我们还有在做个人网站吗?随着自媒体和短视频的发展和兴起,包括我们很多WEB2.0产品的延续,当然也包括个人建站市场的低迷和用户关注的不同,有些个人已经不在做网站。但是,由于我们有些朋友出于网站的爱好或者说是有些项目还是基于PC端网站的,还是有网友抱有信心的,比如我们看到有一些老牌个人网站依旧在运行,且还有新网站的出现。今天在这篇文章中谈谈有网友问关于个人网站备案的问题。这个也是前几天有他在选择...

treeview绑定为你推荐
病历单我想单位请了病假,但是我没病。但单位要必须要病历单,我怎么办?到了医院我说我什么病?mac地址克隆路由器中的MAC地址克隆,是什么,干嘛用的mac地址克隆路由器的Mac地址克隆有什么作用?搜索引擎的概念什么是搜索引擎?搜索引擎是如何工作的?防火墙排名目前比较好的防火墙软件有哪些?鄂n鄂A鄂B鄂C鄂D鄂E鄂F鄂G鄂H鄂J鄂K鄂L鄂M鄂N鄂P鄂Q鄂R鄂S鄂T鄂U分别代表湖北省的哪些城市jstz泰州哪里有民工市场熊猫烧香病毒下载熊猫烧香病毒?什么是生态系统生态系统的功能有什么?labelforhtml中label是什么意思啊?
北京租服务器 hostgator jsp主机 圣迭戈 香港机房托管 商家促销 网通代理服务器 英文站群 坐公交投2700元 admit的用法 爱奇艺vip免费试用7天 福建铁通 net空间 cdn服务 阿里云邮箱怎么注册 上海联通 server2008 gotoassist 热云 qq空间申请关闭 更多