티스토리 뷰

카테고리 없음

c# 트리뷰 만들기 소스코드

뽀로로친구에디 2019. 1. 23. 10:39

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);

            }


        }

댓글
최근에 달린 댓글
글 보관함
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Total
Today
Yesterday
    뽀로로친구에디
    최근에 올라온 글