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

TabbyCloud周年庆&七夕节活动 美國INAP 香港CN2

TabbyCloud迎来一周岁的生日啦!在这一年里,感谢您包容我们的不足和缺点,在您的理解与建议下我们也在不断改变与成长。为庆祝TabbyCloud运营一周年和七夕节,TabbyCloud推出以下活动。TabbyCloud周年庆&七夕节活动官方网站:https://tabbycloud.com/香港CN2: https://tabbycloud.com/cart.php?gid=16购买链...

搬瓦工最新套餐KVM,CN2线路

搬瓦工在国内非常流行的主机商,以提供低价的vps著称.不过近几年价格逐渐攀升.不过稳定性和速度一向不错.依然深受国内vps爱好者喜爱.新上线的套餐经常卖到断货.支持支付宝,paypal很方便购买和使用.官网网站:https://www.bandwagonhost.com[不能直接访问,已墙]https://www.bwh88.net[有些地区不能直接访问]https://www.bwh81.net...

PQS彼得巧 年中低至38折提供台湾彰化HiNet线路VPS主机 200M带宽

在六月初的时候有介绍过一次来自中国台湾的PQS彼得巧商家(在这里)。商家的特点是有提供台湾彰化HiNet线路VPS主机,起步带宽200M,从带宽速率看是不错的,不过价格也比较贵原价需要300多一个月,是不是很贵?当然懂的人可能会有需要。这次年中促销期间,商家也有提供一定的优惠。比如月付七折,年付达到38折,不过年付价格确实总价格比较高的。第一、商家优惠活动年付三八折优惠:PQS2021-618-C...

treeview绑定为你推荐
自动识别查询快递单号查看单号怎么知道是那个快递公司的华为总裁女儿为啥姓孟孟晚舟是谁腾讯年终奖腾讯工作怎么样jstz江苏泰州市地税如何申报?assemblyinfocsgo很跟cs有什么区别云盘网谁知道免费的网盘?xcelsiussap bi是什么微信智能机器人微信群机器人是怎么实现的activitygroupactivityGroup子activity跳转的问题activitygroup请问在activitygroup中嵌入多个webview,切换时如何保持状态
vps优惠码cnyvps 如何查询ip地址 最便宜虚拟主机 花生壳域名贝锐 中国万网域名 域名抢注工具 krypt dns是什么 bluehost l5639 免备案空间 60g硬盘 新世界电讯 tk域名 777te 本网站在美国维护 老左正传 东莞服务器 上海联通宽带测速 帽子云排名 更多