treeview递归加载
实体类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace WindowsFormsApplication2
{
public class China
{
public string AreaCode { get; set; }
public string AreaName { get; set; }
public string ParentAreaCode { get; set; } }
}
数据访问类:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text; namespace WindowsFormsApplication2
{
public class ChinaData
{
SqlConnection conn = null;
SqlCommand cmd = null; public ChinaData()
{
conn = new SqlConnection("server=.;database=mydb;user=sa;pwd=123");
cmd = conn.CreateCommand();
} public List<China> Select(string pcode)
{
List<China> clist = new List<China>(); cmd.CommandText = "select *from ChinaStates where ParentAreaCode = '" + pcode + "'"; conn.Open();
SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows)
{
while (dr.Read())
{
China c = new China();
c.AreaCode = dr[].ToString();
c.AreaName = dr[].ToString();
c.ParentAreaCode = dr[].ToString(); clist.Add(c);
}
} conn.Close();
return clist;
} public List<China> Select()
{
List<China> clist = new List<China>(); cmd.CommandText = "select *from ChinaStates"; conn.Open();
SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows)
{
while (dr.Read())
{
China c = new China();
c.AreaCode = dr[].ToString();
c.AreaName = dr[].ToString();
c.ParentAreaCode = dr[].ToString(); clist.Add(c);
}
} conn.Close();
return clist;
} }
}
form1:treeview 使用递归
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
List<China> AllList = new List<China>(); public Form1()
{
InitializeComponent(); AllList = new ChinaData().Select(); TreeNode tn1 = new TreeNode("中国");
tn1.Tag = ""; NodesBind(tn1); treeView1.Nodes.Add(tn1); } public void NodesBind(TreeNode tn)
{
//lambda 表达式
List<China> clist = AllList.Where(r => r.ParentAreaCode == tn.Tag.ToString()).ToList(); foreach (China c in clist)
{
TreeNode tnn = new TreeNode(c.AreaName);
tnn.Tag = c.AreaCode; NodesBind(tnn); tn.Nodes.Add(tnn);
}
} }
}

treeview递归加载的更多相关文章
- WinForm 进程、线程、TreeView递归加载、发送邮件--2016年12月13日
进程:一个程序就是一个进程,但是也有一个程序需要多个进程来支持的情况 进程要使用的类是:Process它在命名空间:System.Diagnostics; 静态方法Start(); Process.S ...
- winform进程、线程、TreeView递归加载
进程: 一般来说,一个程序就是一个进程,不过也有一个程序需要多个进程支持的情况. 进程所使用的类:Process 所需命名空间:System.Diagnostics; 可以通过进行来开启计算机上现有的 ...
- WinForm TreeView递归加载
这个其实通俗一点讲就是的树状分支图 首先利用递归添加数据 数据放入 treeView1.Nodes.Add() 中 public Form3() { InitializeComponent(); Tr ...
- 省市数据递归加载到TreeView
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 将Xml文件递归加载到TreeView中
#region [通过XDocument的方式将Xml文件递归到TreeView控件中] //读取Xml文件(XDocument) //1.加载Xml文件 XDocument document=XD ...
- C# IO操作(五)文件的递归加载
本篇是一个案例,其核心通过代码展示代码中的递归这个用法,程序的界面如下:
- 递归加载Treeview
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- 仿Windows制作TreeView数据加载
时间有限就直接贴源码吧,理解思路即可. 页面代码: <asp:TreeView ID="TreeViewLeft" runat="server" Show ...
- asp.net treeview 异步加载
在使用TreeView控件的时候,如果数据量太大,这个TreeView控件加载会很慢,有时甚至加载失败, 为了更好的使用TreeView控件加载大量的数据,采用异步延迟加载TreeView. 在Tre ...
随机推荐
- PHP Header下载文件在IE文件名中文乱码问题
解决PHP Header下载文件在IE文件名中文乱码有两种常见的,一种是是把页面编码改成utf8,另一种是对中文url进入urlencode编码,根据UA检测,区别下载,就可以解决了 $filenam ...
- Java虚拟机内存管理机制
自动内存管理机制 Java虚拟机(JVM)在执行Java程序过程中会把它所管理的内存划分为若干个不同的数据区域.这些区域都有各自的用途,以及创建和销毁的时间,有的区域随着虚拟机进程的启动而存在,有的区 ...
- PHP获取POST数据的几种方法汇总
一.PHP获取POST数据的几种方法 方法1.最常见的方法是:$_POST['fieldname']; 说明:只能接收Content-Type: application/x-www-form-urle ...
- ios计算内容的高度 (含7.0前及以后的版本的用法)
+ (CGFloat)heightForContent:(MyMsgTextModel *)content withWidth:(CGFloat)width { CGSize contentSize; ...
- CSS 伪元素&伪类
单冒号(:)用于CSS3伪类,双冒号(::)用于CSS3伪元素 伪元素 属性 描述 CSS :first-letter 向文本的第一个字母添加特殊样式 1 :first-line 向文本的首行添加特殊 ...
- zabbix自动发现监控url
1.在监控客户机上 web_site_code_status.sh: #!/bin/bash UrlFile="/opt/scripts/WEB.txt" IFS=$'\n' we ...
- linux pptpd账号同时登录的问题
最近搞了个云主机搭建个VPN服务器给自己用, 特别是在公共场所的wifi上网时, 很多APP, 或者网站是没有https的, 所以为了保证信息(主要是账号密码)的安全, 搭个私有vpn还是很有必要的. ...
- Wex5页面事件执行顺序
wex5 事件执行顺序data组件的onCustomRefresh→ model组件的onLoad→ windowReceiver组件的onReceive
- elasticsearch客户端连接选择
elasticsearch支持两种协议: http协议. Native Elasticsearch binary protocol(本地elasticsearch二进制协议):elasticsearc ...
- 介绍Android HAL的一篇好文章
从Linux driver到HAL再到JNI再到Java都讲了一个遍,算是对HAL有一个基本的了解了,其中hw_module_t的设计非常巧妙,每个module都会有自己的特定函数,而HAL是不知道的 ...