转载:http://www.jetwu.cn/archives/737
winform treeView 数据绑定
private void Form1_Load(object sender, EventArgs e) { Types = new List<UrlTypes>() { new UrlTypes() {Id = 1, Name = "中国", Value = "0", ParentId = 0}, new UrlTypes() {Id = 2, Name = "河南", Value = "0", ParentId = 1}, new UrlTypes() {Id = 3, Name = "河北", Value = "0", ParentId = 1}, new UrlTypes() {Id = 4, Name = "南阳", Value = "0", ParentId = 2}, new UrlTypes() {Id = 4, Name = "信阳", Value = "0", ParentId = 2}, new UrlTypes() {Id = 5, Name = "新野", Value = "0", ParentId = 4}, new UrlTypes() {Id = 6, Name = "石家庄", Value = "0", ParentId = 3} }; var topNode = new TreeNode(); topNode.Name = "0"; topNode.Text = "世界"; treeView1.Nodes.Add(topNode); Bind(topNode,Types,0); treeView1.ExpandAll(); } private void Bind(TreeNode parNode,List<UrlTypes> list,int nodeId ) { var childList = list.FindAll(t => t.ParentId == nodeId).OrderBy(t=>t.Id); foreach (var urlTypese in childList) { var node = new TreeNode(); node.Name = urlTypese.Id.ToString(); node.Text = urlTypese.Name; parNode.Nodes.Add(node); Bind(node,list,urlTypese.Id); } } public class UrlTypes { public int Id { get; set; } public string Name { get; set; } public string Value { get; set; } public int ParentId { get; set; } }