填充没有ID的树视图

问题描述 投票:-1回答:1

我有如下数据表;

TYPE   NAME

FR     APPLE
FR     BANANA
TR     FISTIK
GR     ENGINE
GR     TANK

我想使用上面的数据表制作如下的树视图;

-FR
 -- APPLE
 -- BANA
-TR
 -- FISTIK
-GR 
 -- ENGINE
 -- TANK

如果可能的话,我想在不使用ID,PARENT_ID的情况下填充它。

那可能吗?

c# winforms datatable treeview
1个回答
1
投票

试试以下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            DataTable dt = new DataTable();
            dt.Columns.Add("Type", typeof(string));
            dt.Columns.Add("NAME", typeof(string));

            dt.Rows.Add(new object[] {"FR", "APPLE"});
            dt.Rows.Add(new object[] {"FR", "BANANA"});
            dt.Rows.Add(new object[] {"TR", "FISTIK"});
            dt.Rows.Add(new object[] {"GR", "ENGINE"});
            dt.Rows.Add(new object[] {"GR", "TANK"});

            populateTreeview(dt);
            treeView1.ExpandAll();
        }
        //Open the XML file, and start to populate the treeview
        private void populateTreeview(DataTable dt)
        {
            var groups = dt.AsEnumerable().GroupBy(x => x.Field<string>("Type"));
            treeView1.Nodes.Clear();

            foreach(var group in groups)
            {
                TreeNode node = treeView1.Nodes.Add(group.Key);
                foreach(string name in group.Select(x => x.Field<string>("NAME")))
                {
                    node.Nodes.Add(name);
                }
            }
        }

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.