码迷,mamicode.com
首页 > Windows程序 > 详细

TreeView控件之,后台构建TreeView(WinForm小程序)

时间:2016-06-07 13:03:08      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

 private void Form1_Load(object sender, EventArgs e)
        {
            //生成DataTable
            DataTable table = new DataTable();
            DataColumn Column;
            DataRow Row;
            Column = new DataColumn();
            Column.ColumnName = "Id";
            table.Columns.Add(Column);
            Column = new DataColumn();
            Column.ColumnName = "Name";
            table.Columns.Add(Column);

            for (int i = 0; i <= 2; i++)
            {
                Row = table.NewRow();
                Row["Id"] = i;
                Row["Name"] = "Name " + i;
                table.Rows.Add(Row);
            }
            if (table.Rows.Count > 0)
            {
                MessageBox.Show("恭喜你,成功后台创建DataTable!!!");
                //绑定TreeView
                foreach(DataRow  row in table.Rows)
                {
                    TreeNode Node = new TreeNode();
                    Node.Text = row["Name"].ToString();
                    this.treeView1.Nodes.Add(Node);
                   // treeView1.Nodes.Add(row["Id"].ToString(),row["Name"].ToString ());
                    foreach (DataRow row1 in table.Rows)
                    {
                        TreeNode node1 = new TreeNode();
                        node1.Text = row1["Name"].ToString();
                        Node.Nodes.Add(node1);
                    }
                }
                this.treeView1.SelectedNode = treeView1.Nodes[0];//默认选中第一个节点
                this.textBox1.Text = this.treeView1.SelectedNode.Text.ToString();
                treeView1.Nodes[0].Expand();
            }
            else
            {
                return;
            }
        }
        //选中节点事件
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            MessageBox.Show("您选中了‘" + treeView1.SelectedNode.Text.ToString() + "");
        }

 

TreeView控件之,后台构建TreeView(WinForm小程序)

标签:

原文地址:http://www.cnblogs.com/zix1314/p/5566536.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!