WinForm TreeView绑定
在类别显示的时候,我们往往会选择树状的形式展现出来,这个很常见,我们来看看在winForm中如何绑定
在winForm的控件中,已经为我们提供了一个树状展示的控件TreeView,这个用起来很方便,只要记住几点就可以很好完成数据的绑定
首先,TreeView的节点是TreeNode,每一个节点都是一个TreeNode对象
其次、完成对每个TreeNode对象的属性绑定
然后、可以通过TreeNode的Nodes.add(treeNode)添加子节点
最后、将节点绑定到TreeVIew上 如:treeView1.Nodes.Add(treeNode);
看看下面的例子就一目了然了
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public void BindTreeView()
{
//根节点
TreeNode treeNode = new TreeNode();
treeNode.Name = "节点名称";
treeNode.Text = "根节点";
//根节点下面的子节点
TreeNode node1 = new TreeNode();
node1.Text = "第二个节点1";
TreeNode node2 = new TreeNode();
node2.Text = "第二个节点2";
TreeNode node3 = new TreeNode();
node3.Text = "第二个节点3";
TreeNode node4 = new TreeNode();
node4.Text = "第二个节点4";
//将节点添加到根节点中
treeNode.Nodes.Add(node1);
treeNode.Nodes.Add(node2);
treeNode.Nodes.Add(node3);
treeNode.Nodes.Add(node4);
//为第二个子节点1添加子节点
TreeNode childNode1=new TreeNode();
childNode1.Text="子节点1-1";
TreeNode childNode2=new TreeNode();
childNode2.Text="子节点1-2";
TreeNode childNode3=new TreeNode();
childNode3.Text="子节点1-3";
node1.Nodes.Add(childNode1);
node1.Nodes.Add(childNode2);
node1.Nodes.Add(childNode3);
//为第二个子节点2添加子节点
TreeNode childNode4 = new TreeNode();
childNode4.Text = "子节点2-1";
TreeNode childNode5 = new TreeNode();
childNode5.Text = "子节点2-2";
TreeNode childNode6 = new TreeNode();
childNode6.Text = "子节点2-3";
node2.Nodes.Add(childNode4);
node2.Nodes.Add(childNode5);
node2.Nodes.Add(childNode6);
//将根节点添加到TreeView中
treeView1.Nodes.Add(treeNode);
treeView1.ExpandAll();
}
{
//根节点
TreeNode treeNode = new TreeNode();
treeNode.Name = "节点名称";
treeNode.Text = "根节点";
//根节点下面的子节点
TreeNode node1 = new TreeNode();
node1.Text = "第二个节点1";
TreeNode node2 = new TreeNode();
node2.Text = "第二个节点2";
TreeNode node3 = new TreeNode();
node3.Text = "第二个节点3";
TreeNode node4 = new TreeNode();
node4.Text = "第二个节点4";
//将节点添加到根节点中
treeNode.Nodes.Add(node1);
treeNode.Nodes.Add(node2);
treeNode.Nodes.Add(node3);
treeNode.Nodes.Add(node4);
//为第二个子节点1添加子节点
TreeNode childNode1=new TreeNode();
childNode1.Text="子节点1-1";
TreeNode childNode2=new TreeNode();
childNode2.Text="子节点1-2";
TreeNode childNode3=new TreeNode();
childNode3.Text="子节点1-3";
node1.Nodes.Add(childNode1);
node1.Nodes.Add(childNode2);
node1.Nodes.Add(childNode3);
//为第二个子节点2添加子节点
TreeNode childNode4 = new TreeNode();
childNode4.Text = "子节点2-1";
TreeNode childNode5 = new TreeNode();
childNode5.Text = "子节点2-2";
TreeNode childNode6 = new TreeNode();
childNode6.Text = "子节点2-3";
node2.Nodes.Add(childNode4);
node2.Nodes.Add(childNode5);
node2.Nodes.Add(childNode6);
//将根节点添加到TreeView中
treeView1.Nodes.Add(treeNode);
treeView1.ExpandAll();
}
下面来看一个将xml文件的数据读取到树上的例子
xml文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="utf-8"?>
<Users>
<User id="0">
<Name>Jeffrey</Name>
<Sex>男</Sex>
<UserName>Jeffrey</UserName>
</User>
<User id="1">
<Name>Tom</Name>
<Sex>男</Sex>
<UserName>tom</UserName>
</User>
<User id="2">
<Name>Jef</Name>
<Sex>男</Sex>
<UserName>jef</UserName>
</User>
<User id="3">
<Name>1</Name>
<Sex>1</Sex>
<UserName>1</UserName>
</User>
</Users>
<Users>
<User id="0">
<Name>Jeffrey</Name>
<Sex>男</Sex>
<UserName>Jeffrey</UserName>
</User>
<User id="1">
<Name>Tom</Name>
<Sex>男</Sex>
<UserName>tom</UserName>
</User>
<User id="2">
<Name>Jef</Name>
<Sex>男</Sex>
<UserName>jef</UserName>
</User>
<User id="3">
<Name>1</Name>
<Sex>1</Sex>
<UserName>1</UserName>
</User>
</Users>
读取并绑定到TreeView上
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public void bindXmltoTreeView()
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(System.Environment.CurrentDirectory + "\\UsersXml.xml");
XmlNodeList xnodeList = xdoc.SelectNodes("//User");
TreeNode parentNode = new TreeNode("根节点");
treeView2.Nodes.Add(parentNode);
foreach (XmlNode node in xnodeList)
{
string name = node.ChildNodes.Item(0).InnerText;
TreeNode tnode = new TreeNode(name);
parentNode.Nodes.Add(tnode);
string Sex = node.ChildNodes.Item(1).InnerText;
TreeNode tnode1 = new TreeNode(Sex);
tnode.Nodes.Add(tnode1);
string UserName = node.ChildNodes.Item(2).InnerText;
TreeNode tnode2 = new TreeNode(UserName);
tnode.Nodes.Add(tnode2);
}
treeView2.ExpandAll();
}
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(System.Environment.CurrentDirectory + "\\UsersXml.xml");
XmlNodeList xnodeList = xdoc.SelectNodes("//User");
TreeNode parentNode = new TreeNode("根节点");
treeView2.Nodes.Add(parentNode);
foreach (XmlNode node in xnodeList)
{
string name = node.ChildNodes.Item(0).InnerText;
TreeNode tnode = new TreeNode(name);
parentNode.Nodes.Add(tnode);
string Sex = node.ChildNodes.Item(1).InnerText;
TreeNode tnode1 = new TreeNode(Sex);
tnode.Nodes.Add(tnode1);
string UserName = node.ChildNodes.Item(2).InnerText;
TreeNode tnode2 = new TreeNode(UserName);
tnode.Nodes.Add(tnode2);
}
treeView2.ExpandAll();
}
当然了,如果是无限极分类,可以使用递归循环就可以了,这里把代码贴出来,以后方便直接拷贝使用,提供编码效率
多思考,多创新,才是正道!