Create an editable XML document (one) and bind the XML document to the Treeview control.

Source: Internet
Author: User
Directory:

Introduction

Bind an xml document to the treeview Control

Filter xml data

Perform drag-and-drop operations

Delete, rename, and insert

Treeview control in use

With xml and xpath, you can easily add drag-and-drop or even more features to your treeview control-by Alex Hildyard

Recently, I have been developing a user interface tool for maintaining online directories. Because this directory contains too many products, it makes sense to classify them using some methods. The directory administrator will need the ability to delete and define new directories, nesting between directories and directories, and cleverly making directories and products look intuitive.

A classification scenario like this urgently requires a hierarchical view by category. First, the triing between data and its representation is usually insignificant (trivial ), because the object model of the treeview control is classified by itself. Second, the ability to expand an independent Tree node will make it easier to browse data at multiple levels. finally, dragging and dropping folders in the TreeView is a simple way to quickly process complex layers and attract attention.

A few minutes later, I realized that the app in my mind is Windows Explorer (windows Resource Manager), and I want to rewrite it, replace the folder with the product directory, and replace the file with the product project, I can even quickly perform operations such as creating or deleting folders and performing drag-and-drop operations. If I write an interface for a relational data later, or write a Contact Management Program, or develop a tool to track my family genealogy, then I will find that I am doing the same thing.

This is meaningless. I need to find a general method for providing hierarchical data sources for the treeview control. This is like a data table control (data grid) you can create, delete, rename, move, and drag and drop data elements in a database, no need to worry about the structure of the data source content in the query

Create an xml document for the treeview control:

According to the hierarchy of treeview, xml is a very logical data format. You can use less than six lines of code to display xml documents in the treeview control, suppose you have an xml document similar to the following, which contains many contact nodes:

<? Xml version = "1.0" encoding = "UTF-8"?>
<Addressbook>
<Contacts id = "Contacts">
<Contact id = "Alex">
<Email id = "popmail">
Someone@some_pop_mail.net </email>
<City> Edinburgh </city>
<Country> United Kingdom </country>
</Contact>
<Contact id = "Rebekah">
<Email id = "webmail">
Someone@some_web_mail.net </email>
<City> Papakura </city>
<Country> New Zealand </country>
</Contact>
<Contact id = "Justin">
<Email id = "webmail">
Someone_else@some_web_mail.com </email>
<City> Muriwai </city>
<Country> New Zealand </country>
</Contact>
</Contacts>
</Addressbook>
You can easily assemble all data elements into the treeview control through recursive calls, and add all XML document nodes to the treeview, you can maintain the node relationship of the xml document by maintaining the treeview control.

[C #]
Private void populateTreeControl (
System. Xml. XmlNode document,
System. Windows. Forms. TreeNodeCollection nodes)
{
Foreach (System. Xml. XmlNode node in
Document. ChildNodes)
{
// If the element has a value, display it;
// Otherwise display the first attribute
// (If there is one) or the element name
// (If there isn' t)
String text = (node. Value! = Null? Node. Value:
(Node. Attributes! = NULL &&
Node. Attributes. Count> 0 )?
Node. attributes [0]. Value: node. Name );
Treenode new_child = new treenode (text );
Nodes. Add (new_child );
Populatetreecontrol (node, new_child.nodes );
}
}

[VB]
Private sub populatetreecontrol (_
Byval document as system. xml. xmlnode ,_
Byval nodes _
System. Windows. Forms. treenodecollection)

Dim node as system. xml. xmlnode
For each node in document. childnodes
'If the element has a value, display it;
'Otherwise display the first attribute
'(If there is one) or the element name
'(If there isn' t)
Dim [text] as string
If node. value <> nothing then
[Text] = node. Value
Else
If not node. attributes is nothing and _
Node. Attributes. Count> 0 then
[Text] = node. attributes (0). Value
Else
[Text] = node. Name
End if
End if

Dim new_child as new treenode ([text])
Nodes. Add (new_child)
Populatetreecontrol (node, new_child.nodes)
Next Node
End sub
Now, you can create a new windows form, drag and drop a Treeview control to the form, and add the following three lines to your data file:

[C #]
System. xml. xmldocument document =
New system. xml. xmldatadocument ();
Document. Load ("../contacts. xml ");
Populatetreecontrol (document. documentelement,
Treeview1.nodes );

[VB]
Dim document as new system. xml. xmldatadocument ()
Document. Load ("../contacts. xml ")
PopulateTreeControl (document. DocumentElement ,_
TreeView1.Nodes)
When you expand the treeview node, you will see Figure 1:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.