c# Winforms 中的分层树视图与大陆、国家、城市
hierarchical treeview in c# Winforms with continent, nation, city
我有一个 SQL table:
REGION NATION CITY
Europe Austria Wien
Europe Austria Graz
APA Australia Sidney
等等……等等……
基本上,地区、国家和城市。
我想构建一个分层的 Treeview,例如:
-EUROPE
--Austria
---Graz
---Wien
-APA
--Australia
---Sydney
我使用 Datatable 从数据库中获取数据。
有人可以帮助我完成循环 FOR 和各种嵌套 if 吗?
非常感谢
尝试以下操作:
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 WindowsFormsApplication51
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
try
{
DataTable dt = new DataTable();
dt.Columns.Add("REGION", typeof(string));
dt.Columns.Add("NATION", typeof(string));
dt.Columns.Add("CITY", typeof(string));
dt.Rows.Add(new object[] { "Europe", "Austria", "Wien" });
dt.Rows.Add(new object[] { "Europe", "Austria", "Graz" });
dt.Rows.Add(new object[] { "APA", "Australia", "Sidney" });
foreach (var region in dt.AsEnumerable().GroupBy(x => x.Field<string>("REGION")))
{
TreeNode regionNode = new TreeNode(region.Key);
treeView1.Nodes.Add(regionNode);
foreach (var nation in region.GroupBy(x => x.Field<string>("NATION")))
{
TreeNode nationNode = new TreeNode(nation.Key);
regionNode.Nodes.Add(nationNode);
foreach (string city in nation.Select(x => x.Field<string>("CITY")))
{
TreeNode cityNode = new TreeNode(city);
nationNode.Nodes.Add(cityNode);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
treeView1.ExpandAll();
}
}
}
我有一个 SQL table:
REGION NATION CITY
Europe Austria Wien
Europe Austria Graz
APA Australia Sidney
等等……等等……
基本上,地区、国家和城市。
我想构建一个分层的 Treeview,例如:
-EUROPE
--Austria
---Graz
---Wien
-APA
--Australia
---Sydney
我使用 Datatable 从数据库中获取数据。
有人可以帮助我完成循环 FOR 和各种嵌套 if 吗? 非常感谢
尝试以下操作:
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 WindowsFormsApplication51
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
try
{
DataTable dt = new DataTable();
dt.Columns.Add("REGION", typeof(string));
dt.Columns.Add("NATION", typeof(string));
dt.Columns.Add("CITY", typeof(string));
dt.Rows.Add(new object[] { "Europe", "Austria", "Wien" });
dt.Rows.Add(new object[] { "Europe", "Austria", "Graz" });
dt.Rows.Add(new object[] { "APA", "Australia", "Sidney" });
foreach (var region in dt.AsEnumerable().GroupBy(x => x.Field<string>("REGION")))
{
TreeNode regionNode = new TreeNode(region.Key);
treeView1.Nodes.Add(regionNode);
foreach (var nation in region.GroupBy(x => x.Field<string>("NATION")))
{
TreeNode nationNode = new TreeNode(nation.Key);
regionNode.Nodes.Add(nationNode);
foreach (string city in nation.Select(x => x.Field<string>("CITY")))
{
TreeNode cityNode = new TreeNode(city);
nationNode.Nodes.Add(cityNode);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
treeView1.ExpandAll();
}
}
}