实体类:

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递归加载的更多相关文章

  1. WinForm 进程、线程、TreeView递归加载、发送邮件--2016年12月13日

    进程:一个程序就是一个进程,但是也有一个程序需要多个进程来支持的情况 进程要使用的类是:Process它在命名空间:System.Diagnostics; 静态方法Start(); Process.S ...

  2. winform进程、线程、TreeView递归加载

    进程: 一般来说,一个程序就是一个进程,不过也有一个程序需要多个进程支持的情况. 进程所使用的类:Process 所需命名空间:System.Diagnostics; 可以通过进行来开启计算机上现有的 ...

  3. WinForm TreeView递归加载

    这个其实通俗一点讲就是的树状分支图 首先利用递归添加数据 数据放入 treeView1.Nodes.Add() 中 public Form3() { InitializeComponent(); Tr ...

  4. 省市数据递归加载到TreeView

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. 将Xml文件递归加载到TreeView中

    #region [通过XDocument的方式将Xml文件递归到TreeView控件中] //读取Xml文件(XDocument) //1.加载Xml文件 XDocument  document=XD ...

  6. C# IO操作(五)文件的递归加载

    本篇是一个案例,其核心通过代码展示代码中的递归这个用法,程序的界面如下:

  7. 递归加载Treeview

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  8. 仿Windows制作TreeView数据加载

    时间有限就直接贴源码吧,理解思路即可. 页面代码: <asp:TreeView ID="TreeViewLeft" runat="server" Show ...

  9. asp.net treeview 异步加载

    在使用TreeView控件的时候,如果数据量太大,这个TreeView控件加载会很慢,有时甚至加载失败, 为了更好的使用TreeView控件加载大量的数据,采用异步延迟加载TreeView. 在Tre ...

随机推荐

  1. bootstrap加深

    1.安装: bootstrap中文网:http://www.bootcss.com/ bootstrap.css样式:http://v3.bootcss.com/css/#tables class=' ...

  2. 阅读廖雪峰老师git教程笔记

    1.首先git是目前世界上最先进的分布式版本控制系统之一.所谓版本控制是针对工作中一些普遍的现象的. 比如,你写一份文档,期间,不断的改善,每次修改都会进行备份,久而久之,会有很多版本的同一份文档,但 ...

  3. SynchronousQueue 的简单应用

    SynchronousQueue是这样一种阻塞队列,其中每个 put 必须等待一个 take,反之亦然.同步队列没有任何内部容量,甚至连一个队列的容量都没有.      不能在同步队列上进行 peek ...

  4. ngModel

    https://docs.angularjs.org/error/ngModel/numfmt?p0=sa angular.module('myApp', []) .directive('tagLis ...

  5. LeetCode Factor Combinations

    原题链接在这里:https://leetcode.com/problems/factor-combinations/ 题目: Numbers can be regarded as product of ...

  6. mysql 三种恢复方式

    为了保障数据的安全,需要定期对数据进行备份.备份的方式有很多种,效果也不一样.一旦数据库中的数据出现了错误,就需要使用备份好的数据进行还原恢复.从而将损失降到最低.下面我们来了解一下MySQL常见的有 ...

  7. 《奥威Power-BI智能分析报表制作方法》精彩回顾

    年的最后一个月,一年又快过去.工作和学习都不能耽误,本周三奥威公开课又如约与大家见面咯!不知老师教的图文报表在课后你们都有练习吗?趁热打铁,我们现在再次来温习一下吧. 本期分享的内容:<奥威Po ...

  8. iOS逆传值的三种方式

    1.代理 2.block 2.通知中心

  9. 三大框架-Hibernate

    概念 持久化框架 把对象保存到数据库中,对数据的CURD操作 ORM框架 ORM对象关系映射 类<->表 属性<->字段 对象<->表中的记录 实现方法 创建持久化 ...

  10. JS按回车键实现登录的方法

    本文实例讲述了JS按回车键实现登录的方法,该功能有着非常广泛的实用价值.分享给大家供大家参考之用.具体方法如下: 方法一: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 < ...