C # Use of winform treelistview (treegridview ),

Source: Internet
Author: User

C # Use of winform treelistview (treegridview ),

The TreeView control displays a single set of content. It is a good choice to display more detailed information about TreeListView.

First look at the effect:

First, you must reference the file System. Windows. Forms. TreeListView. dll, System. Runtime. InteropServices. APIs. dll.

You can add TreeListView to the toolbox and add it to the form.

1. You need to add columns

2. You need to add an ImageList container as the node chart subject (you also need to configure the SmallImageList attribute of TreeListView as the ID of the ImageList Control)

3. Now you can bind data to the control.

This control is suitable for displaying complex data structures with parent-child relationships, including XML data.

Next we try to parse a Device Tree XML and then bind it to the control:

<Device name="hidc-1600tv _192.168.230.188" ItemType="DVR" type="Onvif" TypeID="" Code="" location="" Description="" ID="" UniqueID="192.168.230.188">  <IP Value="192.168.230.188" />  <Port Value="80" />  <Username Value="admin" />  <Password Value="1234" />  <AuthenAddress Value="/" />  <AuthenMode Value="1" />  <OnvifUser Value="admin" />  <OnvifPwd Value="1234" />  <OnvifAddress Value="/onvif/device_service" />  <RTSPUser Value="admin" />  <RTSPPwd Value="1234" />  <ChildDevices>    <Device name="" ItemType="Channel" type="" TypeID="" Code="" location="" Description="" id="" UniqueID="">      <PTZEnable Value="True" />      <PTZ1 Value="5" />      <PTZ2 Value="15" />      <PTZ3 Value="25" />      <PTZ4 Value="35" />      <PTZ5 Value="45" />      <PTZ6 Value="55" />      <PTZ7 Value="65" />      <PTZ8 Value="75" />      <PTZ9 Value="85" />      <ChildDevices>        <Device name="" ItemType="RStreamer" type="" TypeID="1" Code="" location="" Description="" id="">          <MediaProfile Value="1" />          <Multicast Value="False" />        </Device>        <Device name="" ItemType="RStreamer" type="" TypeID="2" Code="" location="" Description="" id="">          <MediaProfile Value="2" />          <Multicast Value="False" />        </Device>      </ChildDevices>    </Device>  </ChildDevices></Device>

Recursive algorithms are used to easily extract XML structures.

        public void LoadXmlTree(string xml)        {            XDocument xDoc = XDocument.Parse(xml);            TreeListViewItem item = new TreeListViewItem();            string title = xDoc.Root.Attribute("name")?.Value ?? xDoc.Root.Name.LocalName;            item.Text = title;            item.ImageIndex = 0;            item.SubItems.Add(xDoc.Root.Attribute("UniqueID")?.Value);            item.SubItems.Add(xDoc.Root.Attribute("ItemType")?.Value);            PopulateTree (xDoc.Root, item.Items);            tvDevice.Items.Add(item);        }        public void PopulateTree (XElement element, TreeListViewItemCollection items)        {            foreach (XElement node  in element.Nodes())            {                TreeListViewItem item = new TreeListViewItem();                string title = node.Name.LocalName.Trim();                item.Text = title;                if (title == "Device")                {                    var attr = node.Attribute("ItemType")?.Value;                    switch (attr)                    {                        case "Channel": item.ImageIndex = 1; break;                        case "RStreamer": item.ImageIndex = 3; break;                        default: break;                    }                    item.SubItems.Add(node.Attribute("UniqueID")?.Value);                    item.SubItems.Add(node.Attribute("ItemType")?.Value);                }                else                {                    item.ImageIndex = 2;                    item.SubItems.Add(node.Attribute("Value")?.Value);                }                if (node.HasElements)                {                    PopulateTree (node, item.Items);                }                items.Add(item);            }        }    

Note:

TreeListViewItem can be used to construct input values and imageindex. value is assigned to the Text attribute, and imageindex is the index of ImageList corresponding to the icon displayed on the node. The SubItems of TreeListViewItem is its extended column, which is displayed in the following columns in sequence.

You can set the ExpandMethod attribute to control the node expansion mode and set the CheckBoxes attribute to control whether to display the check box.

You can subscribe to the BeforeExpand, BeforeCollapse, and BeforeLabelEdit events to modify icons in different states, for example:

        private void treeListView1_BeforeExpand(object sender,  TreeListViewCancelEventArgs e)        {            if(e.Item.ImageIndex == 0) e.Item.ImageIndex = 1;        }

You can set the LabelEdit attribute to activate or disable editing. The TreeListViewBeforeLabelEditEventArgs parameter provides the corresponding attribute values.

        private void treeListView1_BeforeLabelEdit(object sender, TreeListViewBeforeLabelEditEventArgs e)        {            if(e.ColumnIndex == 1)            {                ComboBox combobox = new ComboBox();                combobox.Items.AddRange(new string[]{"Html","Css","Javascript"});                e.Editor = combobox;            }        }

You can also modify the Open Source TreeListView as needed.

Source: http://www.cnblogs.com/liuxiaobo93/p/7942619.html

Attachment download: TreeListView

Related Article

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.