treeview控件使用+连接数据库库

 
//没有理由不坚持,慢慢走吧,后面的路或许很难,别放弃!
private void Form3_Load(object sender, EventArgs e) { //添加一个根节点 TreeNode root = new TreeNode("游戏类型"); treeView1.Nodes.Add(root); //在数据库查找需要的类型,生成子节点 string sql = "select * from Categories"; DataTable table = DBHelper.Query(sql); //循环查找出数据库的类型,生成子节点 foreach (DataRow row in table.Rows) { string name = row["Name"].ToString(); //int类型储存id int id = (int)row["ID"]; TreeNode node = new TreeNode(name); node.Tag = id; //初始值为0 root.Tag = 0; root.Nodes.Add(node); } //让treeview试图在界面打开时全部展开 treeView1.ExpandAll(); } //在点击对应的节点在datagridview的控件中显示相应的试图 private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { int id=(int)(treeView1.SelectedNode.Tag); int cateid=id; string sql = string.Format("select * from Products where CategoryID='{0}'", cateid); dataGridView1.DataSource = DBHelper.Query(sql); } } }

 

posted on 2021-06-24 21:59  钟硕cool  阅读(228)  评论(0编辑  收藏  举报

导航