转载:http://www.jetwu.cn/archives/737
winform treeView 数据绑定
private void Form1_Load(object sender, EventArgs e)
{
Types = new List<UrlTypes>()
{
new UrlTypes() {Id = , Name = "中国", Value = "", ParentId = },
new UrlTypes() {Id = , Name = "河南", Value = "", ParentId = },
new UrlTypes() {Id = , Name = "河北", Value = "", ParentId = },
new UrlTypes() {Id = , Name = "南阳", Value = "", ParentId = },
new UrlTypes() {Id = , Name = "信阳", Value = "", ParentId = },
new UrlTypes() {Id = , Name = "新野", Value = "", ParentId = },
new UrlTypes() {Id = , Name = "石家庄", Value = "", ParentId = }
}; var topNode = new TreeNode();
topNode.Name = "";
topNode.Text = "世界";
treeView1.Nodes.Add(topNode);
Bind(topNode,Types,); 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; }
}