C#动态递归绑定TreeView(WinForm)

定义一个单位数据表,用来存储单位信息,程序运行时要从数据库里取出单位信息动态绑定在 TreeView 树形菜单。这个数据表的结构是这样的:

下面是相关的程序代码:

 
  1:         OleDbConnection connection = null;
  2:         OleDbCommand command = null;
  3:         OleDbDataAdapter dataAdapter = null;
  4:         string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " +
  5:                     Application.StartupPath + @"\Database\TEST.mdb" + "; Jet OleDb:Database Password = 12345";
  6:
  7:         private void FormMain_Load(object sender, EventArgs e)
  8:         {
  9:                 //初始化TreeView信息
 10:                 TreeViewUnit_DataBind();
 11:         }
 12:         protected void TreeViewUnit_DataBind()
 13:         {
 14:
 15:             //初始化连接
 16:             connection = new OleDbConnection(connectionString);
 17:             //打开连接
 18:             connection.Open();
 19:
 20:             //初始化TreeView信息
 21:             this.treeViewUnit.Nodes.Clear();
 22:             TreeNode root = new TreeNode("单位列表");     //添加根节点
 23:             root.Expand();
 24:             this.treeViewUnit.Nodes.Add(root);
 25:
 26:             string sqlStr = "SELECT * FROM UNIT WHERE SuperiorID = 0";  //选择大单位节点
 27:
 28:             dataAdapter = new OleDbDataAdapter(sqlStr, connection);
 29:             DataSet ds = new DataSet();
 30:             dataAdapter.Fill(ds);
 31:             DataView dv = ds.Tables[0].DefaultView;
 32:
 33:             foreach (DataRowView row in dv)
 34:             {
 35:                 string unitName = row["Unit_Name"].ToString().Trim();
 36:                 int unitID = Convert.ToInt32(row["ID"].ToString().Trim());
 37:                 TreeNode unit=new TreeNode(unitName);
 38:                 unit.Expand();
 39:
 40:                 TreeView_AddChildNodes(unit, unitID);
 41:                 this.treeViewUnit.Nodes.Add(unit);
 42:             }
 43:
 44:             //关闭连接
 45:             connection.Close();
 46:         }
 47:
 48:         //TreeView递归添加子节点
 49:         protected void TreeView_AddChildNodes(TreeNode node, int nodeID)
 50:         {
 51:             string sqlStr="SELECT * FROM UNIT WHERE SuperiorID = " + nodeID;
 52:             dataAdapter = new OleDbDataAdapter(sqlStr, connection);
 53:             DataSet ds = new DataSet();
 54:             dataAdapter.Fill(ds);
 55:             DataView dv = ds.Tables[0].DefaultView;
 56:
 57:             foreach (DataRowView row in dv)
 58:             {
 59:                 string unitName = row["Unit_Name"].ToString().Trim();
 60:                 int unitID = Convert.ToInt32(row["ID"].ToString().Trim());
 61:                 TreeNode unit = new TreeNode(unitName);
 62:                 unit.Expand();
 63:
 64:                 TreeView_AddChildNodes(unit, unitID);
 65:                 node.Nodes.Add(unit);
 66:             }
 67:         }

        程序的运行结果是这样的,满足了我的需求。因为是小程序,数据也比较少,所以没有考虑数据库存储优化等等。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值