2023年6月22日发(作者:)
详解TreeView绑定数据库很多应⽤要⽤到TreeView来显⽰组织机构,以下演⽰TreeView如何与数据库进⾏绑定。数据库结构如下(递归现实):id(guid) pid(guid) name18a83618-8751-47ef-91a0-e2dcde42bb71 **公司c775c004-48ed-4664-8b0c-8fd26fea5f4e 18a83618-8751-47ef-91a0-e2dcde42bb71 A部门a43696f0-a906-4b4a-8abf-a01fb7d54daf c775c004-48ed-4664-8b0c-8fd26fea5f4e A部门->1班组0d7fb83a-c056-482e-800b-52e20c74791b c775c004-48ed-4664-8b0c-8fd26fea5f4e A部门->2班组de28685a-aaff-4876-abe1-bb003d17db64 18a83618-8751-47ef-91a0-e2dcde42bb71 B部门绑定到TreeView的最终效果如下图:1、新建⼀个TreeView控件⼆、绑定2.1 传统做法(⾏不通)2.2 正确做法:⾃⼰建⽴⼀个递归的⽅法。2.2.1 递归其实就是⽅法的重复调⽤,下⾯是我总结的两种⽅法,对于初学者来说不明⽩其中的代码也没关系,直接拷贝下来⽤就可以了。 #region 绑定TreeView /// /// 绑定TreeView(利⽤TreeNode) /// /// TreeNode(TreeView的⼀个节点) /// ⽗id的值 /// 数据库 id 字段名 /// 数据库 ⽗id 字段名 /// 数据库 ⽂本 字段值 protected void Bind_Tv(DataTable dt,TreeNode p_Node, string pid_val, string id, string pid, string text) { DataView dv = new DataView(dt);//将DataTable存到DataView中,以便于筛选数据 TreeNode tn;//建⽴TreeView的节点(TreeNode),以便将取出的数据添加到节点中 //以下为三元运算符,如果⽗id为空,则为构建“⽗id字段 is null”的查询条件,否则构建“⽗id字段=⽗id字段值”的查询条件 string filter = OrEmpty(pid_val) ? pid+" is null" : (pid+"='{0}'", pid_val); ter = filter;//利⽤DataView将数据进⾏筛选,选出相同 ⽗id值 的数据 foreach (DataRowView row in dv) { tn = new TreeNode();//建⽴⼀个新节点(学名叫:⼀个实例) if (p_Node==null)//如果为根节点 { = row[id].ToString();//节点的Value值,⼀般为数据库的id值 = row[text].ToString();//节点的Text,节点的⽂本显⽰ (tn);//将该节点加⼊到TreeView中 Bind_Tv(dt,tn, , id, pid, text);//递归(反复调⽤这个⽅法,直到把数据取完为⽌) } else//如果不是根节点 { = row[id].ToString();//节点Value值 = row[text].ToString();//节点Text值 p_(tn);//该节点加⼊到上级节点中 Bind_Tv(dt,tn, , id, pid, text);//递归 } } }
/// /// 绑定TreeView(利⽤TreeNodeCollection) /// /// TreeNodeCollection(TreeView的节点集合) /// ⽗id的值 /// 数据库 id 字段名 /// 数据库 ⽗id 字段名 /// 数据库 ⽂本 字段值 private void Bind_Tv(DataTable dt,TreeNodeCollection tnc, string pid_val, string id, string pid, string text) { DataView dv = new DataView(dt);//将DataTable存到DataView中,以便于筛选数据 TreeNode tn;//建⽴TreeView的节点(TreeNode),以便将取出的数据添加到节点中 //以下为三元运算符,如果⽗id为空,则为构建“⽗id字段 is null”的查询条件,否则构建“⽗id字段=⽗id字段值”的查询条件 string filter = OrEmpty(pid_val) ? pid + " is null" : (pid + "='{0}'", pid_val); ter = filter;//利⽤DataView将数据进⾏筛选,选出相同 ⽗id值 的数据 foreach (DataRowView drv in dv) { tn = new TreeNode();//建⽴⼀个新节点(学名叫:⼀个实例) = drv[id].ToString();//节点的Value值,⼀般为数据库的id值 = drv[text].ToString();//节点的Text,节点的⽂本显⽰ (tn);//将该节点加⼊到TreeNodeCollection(节点集合)中 Bind_Tv(dt, odes, , id, pid, text);//递归(反复调⽤这个⽅法,直到把数据取完为⽌) } } #endregion2.2.2 调⽤2.2.3 关于数据—》测试数据(作⽤:模拟⼀个真实的数据库表),这个⽅法的作⽤是建⽴⼀个表,表中有三个字段,分别为id、⽗id、名称,然后往⾥⾯插⼊些数据。
private DataTable Test_Table() { DataTable dt = new DataTable(); DataRow dr; (new DataColumn("id", typeof(Guid)));//id列 类型guid (new DataColumn("parent_id", typeof(Guid)));//⽗id列 类型guid (new DataColumn("name", typeof(string)));//名称列 类型string //构造 公司 根节点 dr = (); var node0=dr[0] = d(); dr[1] = ; dr[2] = "** 公司"; (dr); //构造 部门 节点 string[] department = { "A部门", "B部门", "C部门"}; for (int i = 0; i < ; i++) { dr = (); var node1=dr[0] = d(); dr[1] = node0;//(部门节点)属于公司根节点 dr[2] = department[i]; (dr); //构造 班组 节点 for (int j = 1; j < 4; j++) { dr = (); dr[0] = d(); dr[1] = node1; dr[2] = j+"班组"; (dr); } } return dt; }—》真实数据 我们要在数据库中建⽴三个字段 分别为id、⽗id、名称,然后向⾥⾯插⼊数据,需要注意的就是第⼀⾏数据的⽗id要设置为空。 然后调⽤如下⽅法把数据库中的数据取出。
/// /// 取出数据库中数据,⽣成DataTable /// /// 数据库连接 /// sql语句 /// private DataTable exe_Table(string str_Con,string str_Cmd) { DataSet ds = new DataSet(); using (OracleConnection conn = new OracleConnection(str_Con)) { using (OracleDataAdapter oda = new OracleDataAdapter(str_Cmd, conn)) { (); (ds); } } return [0]; }最后在 !IsPostBack中调⽤ if (!IsPostBack) { Bind_Tv(exe_Table("连接字符串", "select * from 表"), , null, "id字段", "⽗id字段", "名称字段"); }
以上就是本⽂的全部内容,希望对⼤家熟练掌握TreeView绑定数据库有所帮助。
发布者:admin,转转请注明出处:http://www.yc00.com/news/1687382350a5894.html
评论列表(0条)