我有如下的数据表;
TYPE NAME
FR APPLE
FR BANANA
TR FISTIK
GR ENGINE
GR TANK
我想使用上面的数据表制作如下所示的树视图;
-FR
-- APPLE
-- BANA
-TR
-- FISTIK
-GR
-- ENGINE
-- TANK
如果可能,我想在不使用ID,PARENT_ID的情况下填充此内容。
那可能吗?
答案 0 :(得分: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);
}
}
}
}
}