宏盛

递归加载Treeview

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 递归城市实例
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
LoadData(treeView1.Nodes, GetArea(0));

}

private void LoadData(TreeNodeCollection treeNodeCollection, List<T_Area> list)
{
foreach (T_Area item in list)
{
TreeNode node = treeNodeCollection.Add(item.AreaName);
node.Tag = item.AreaId;
LoadData(node.Nodes, GetArea(item.AreaId));

}
}
//加载父类省
public List<T_Area> GetArea(int pid)

{
List<T_Area> list=new List<T_Area>();
string sql = "select AreaId,AreaName from T_Area where AreaPid = @pid";
using (SqlDataReader reader= SqlHelper.ExecuteReader(sql,CommandType.Text, new SqlParameter(parameterName:"@pid", value:pid)))

{
if (reader.HasRows)
{
while (reader.Read())
{
T_Area model = new T_Area();
model.AreaId = reader.GetInt32(0);
model.AreaName = reader.GetString(1);
list.Add(model);
}
}

}
return list;

}
}
}

posted on 2017-05-02 15:37  宏盛  阅读(218)  评论(0编辑  收藏  举报