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

月神科技:香港CN2/洛杉矶CN2/华中电信高防vps,月付20元起

月神科技怎么样?月神科技是由江西月神科技有限公司运营的一家自营云产品的IDC服务商,提供香港安畅、香港沙田、美国CERA、华中电信等机房资源,月神科技有自己的用户群和拥有创宇认证,并且也有电商企业将业务架设在月神科技的平台上。目前,香港CN2云服务器、洛杉矶CN2云主机、华中电信高防vps,月付20元起。点击进入:月神科技官方网站地址月神科技vps优惠信息:香港安畅CN2-GIA低至20元核心:2...

什么是BGP国际线路及BGP线路有哪些优势

我们在选择虚拟主机和云服务器的时候,是不是经常有看到有的线路是BGP线路,比如前几天有看到服务商有国际BGP线路和国内BGP线路。这个BGP线路和其他服务线路有什么不同呢?所谓的BGP线路机房,就是在不同的运营商之间通过技术手段时间各个网络的兼容速度最佳,但是IP地址还是一个。正常情况下,我们看到的某个服务商提供的IP地址,在电信和联通移动速度是不同的,有的电信速度不错,有的是移动速度好。但是如果...

HyperVMart:加拿大vps,2核/3G/25G NVMe/G口不限流量/季付$10.97,免费Windows系统

hypervmart怎么样?hypervmart是一家成立了很多年的英国主机商家,上一次分享他家还是在2年前,商家销售虚拟主机、独立服务器和VPS,VPS采用Hyper-V虚拟架构,这一点从他家的域名上也可以看出来。目前商家针对VPS有一个75折的优惠,而且VPS显示的地区为加拿大,但是商家提供的测速地址为荷兰和英国,他家的优势就是给到G口不限流量,硬盘为NVMe固态硬盘,这个配置用来跑跑数据非常...

treeview绑定为你推荐
donghang东航空姐工资多少钱一个月 东航空乘收入细则qq业务查询怎么查询别人为我开通的QQ业务?iso20000认证为什么ISO20000认证能够风靡全球?它对整个企业的发展有什么好处?以图搜人怎样人肉搜人?只知道他名字和当初居住的地址视频技术视频编辑是干什么的,主要是做一些什么工作呢?这个职位好不好?发展前景怎么样?腾讯公告官网公告是什么12种颜色12种颜色都是代表什么数据统计分析表如何用Excel做数据分析?activitygroupActivityGroup子activity之间的切换效果怎么实现腾讯贴吧QQ贴吧如何发帖
域名邮箱 域名反查 中国万网域名 5折 狗爹 私人服务器 免备案cdn php主机 国外空间服务商 申请个人网页 英文站群 网通ip 上海域名 anylink 支付宝扫码领红包 根服务器 便宜空间 lick 智能dns解析 卡巴斯基试用版下载 更多