标签:
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