티스토리 뷰
private void Treeview_Load()
{
treeView1.Nodes.Clear();
ImageList imgList = new ImageList();
imgList.Images.Add(Bitmap.FromFile("red.png"));
imgList.Images.Add(Bitmap.FromFile("green.png"));
imgList.Images.Add(Bitmap.FromFile("grey.png"));
treeView1.ImageList = imgList;
MySqlConnection myConn = new MySqlConnection("datasource=localhost;port=3306;username=root;password=1234");
MySqlDataAdapter myadp = new MySqlDataAdapter("select DISTINCT line from tcm_data.tc2 where isSelected='1';", myConn);
DataTable dt = new DataTable();
myadp.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
TreeNode parentNode = treeView1.Nodes.Add("LINE", dr["line"].ToString(), 2, 2);
PopulateTreeView((dr["line"].ToString()), parentNode);
}
treeView1.ExpandAll();
}
private void PopulateTreeView(string parentId, TreeNode parentNode)
{
MySqlConnection myConn = new MySqlConnection("datasource=localhost;port=3306;username=root;password=1234");
MySqlDataAdapter myadp = new MySqlDataAdapter("select DISTINCT block from tcm_data.tc2 where isSelected='1' and line = '" + parentId + "';", myConn);
DataTable dtchild = new DataTable();
myadp.Fill(dtchild);
TreeNode childNode;
foreach (DataRow dr in dtchild.Rows)
{
if (parentNode == null)
childNode = treeView1.Nodes.Add("BLOCK", dr["block"].ToString(), 2, 2);
else
childNode = parentNode.Nodes.Add("BLOCK", dr["block"].ToString(), 2, 2);
PopulateTreeView2((dr["block"].ToString()), childNode);
}
}
private void PopulateTreeView2(string parentId, TreeNode parentNode)
{
MySqlConnection myConn = new MySqlConnection("datasource=localhost;port=3306;username=root;password=1234");
MySqlDataAdapter myadp = new MySqlDataAdapter("select DISTINCT robot from tcm_data.tc2 where isSelected='1' and block = '" + parentId + "';", myConn);
DataTable dtchild = new DataTable();
myadp.Fill(dtchild);
TreeNode childNode;
foreach (DataRow dr in dtchild.Rows)
{
if (parentNode == null)
childNode = treeView1.Nodes.Add("ROBOT", dr["robot"].ToString(), 0, 1);
else
childNode = parentNode.Nodes.Add("ROBOT", dr["robot"].ToString(), 0, 1);
PopulateTreeView((dr["robot"].ToString()), childNode);
}
}