一、WinForm中TreeView数据绑定
2011-06-22 15:27
531 查看
原问题贴地址:http://topic.csdn.net/u/20110621/17/7bad3c94-2761-4d39-84fa-db95b8e66977.html
部门表
bumenId 部门
1 产品研发部
2 工程项目部
3 行政部
4 市场部
用户表
userId 用户 bumenId
1 张三 1
2 李四 1
3 王五 2
4 余六 2
5 田七 3
6 朱八 3
7 叶九 4
8 姚个 4
输出结果如下:
二、另一种方式,数据表结果如下图:
部门表
bumenId 部门
1 产品研发部
2 工程项目部
3 行政部
4 市场部
用户表
userId 用户 bumenId
1 张三 1
2 李四 1
3 王五 2
4 余六 2
5 田七 3
6 朱八 3
7 叶九 4
8 姚个 4
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using System.Data; namespace CSDNDemoTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { DataSet ds_Department = getDataSet("部门表"); DataSet ds_Employees = getDataSet("用户表"); foreach (DataRow dr in ds_Department.Tables[0].Rows) { //部门表绑定,作为一级层次 TreeNode tn_origine = new TreeNode(); tn_origine.Text = dr["部门"].ToString(); this.treeView1.Nodes.Add(tn_origine); //用户表绑定 DataRow[] dr_arr = ds_Employees.Tables[0].Select("bumenId="+int.Parse(dr["bumenId"].ToString())); if (dr_arr.Length > 0) { foreach (DataRow dr_sub in dr_arr) { TreeNode tn_sub = new TreeNode(); tn_sub.Text = dr_sub["用户"].ToString(); tn_origine.Nodes.Add(tn_sub); } } } } //获取数据集 public DataSet getDataSet(string tableName) { DataSet ds = new DataSet(); using (SqlConnection con = new SqlConnection("Data Source=localhost;uid=sa;pwd=saiyang;Database=CSDN")) { con.Open(); string strSQL = "select * from "+tableName; using (SqlDataAdapter sda = new SqlDataAdapter(strSQL, con)) { sda.Fill(ds); } } return ds; } } }
输出结果如下:
二、另一种方式,数据表结果如下图:
//实现多级目录 public string rootFT_Id = "00";//根节点Tag // 添加根节点 private void AddRootCompany() { DataSet ds = new DataSet(); using (SqlConnection con = new SqlConnection("Data Source=localhost;uid=sa;pwd=;Database=TW_KJ")) { con.Open(); string strSQL = "select * from TB_Personnel_Type where FT_ID =" + rootFT_Id; using (SqlDataAdapter adapter = new SqlDataAdapter(strSQL, con)) { adapter.Fill(ds); } TreeNode NewNode = new TreeNode(); NewNode.Text = ds.Tables[0].Rows[0]["FT_NAME"].ToString().Trim(); this.treeView1.Nodes.Add(NewNode); InitTreeCompanyChildNode(NewNode, rootFT_Id); } } public DataSet getMenuByLevel(string strFT_ID) { DataSet ds = new DataSet(); using (SqlConnection con = new SqlConnection("Data Source=localhost;uid=sa;pwd=;Database=TW_KJ")) { con.Open(); string strSQL; if (strFT_ID == "00") { strSQL = "select * from TB_Personnel_Type where FT_ID like'" + "0_' and FT_UP_NO = 1"; } else { strSQL = "select * from TB_Personnel_Type where FT_ID like'" + strFT_ID.Trim() + "__'"; } using (SqlDataAdapter adapter = new SqlDataAdapter(strSQL, con)) { adapter.Fill(ds); } } return ds; } //递归获取子节点 private void InitTreeCompanyChildNode(TreeNode pNode, string fatherFT_ID) { DataSet ds = getMenuByLevel(fatherFT_ID); DataView dataView = new DataView(); dataView = ds.Tables[0].DefaultView; foreach (DataRowView drv in dataView) { string newFT_ID = drv["FT_ID"].ToString(); string name = drv["FT_NAME"].ToString(); TreeNode NewNode = new TreeNode(); //将子节点添加到父节点下面 NewNode.Text = name; pNode.Nodes.Add(NewNode); InitTreeCompanyChildNode(NewNode, newFT_ID); } } private void Property_Load(object sender, EventArgs e) { AddRootCompany(); }
相关文章推荐
- winform treeView 数据绑定
- WinForm中动态绑定TreeView数据
- winform中的TreeView的数据绑定
- VB.Net,WinForm中层次数据绑定TreeView的实现
- winform里的treeview能够跟数据绑定吗?我不知道。里面有treeview的用法。是转来的
- TreeView的数据绑定(winForm)
- winform treeView 数据绑定
- 绑定数据到TreeView
- c# 模仿 vue 实现 winform 的数据模型双向绑定
- c#(winform)中自定义ListItem类方便ComboBox和ListBox添加项完全解决(DropDownList 和Combox 数据绑定的常用方法)
- TreeView的数据绑定和遍历
- Winform中的Treeview动态绑定数据库
- 【原创】SQL数据库数据绑定到TreeView菜单目录的类方法实现
- C# winform 中如何设定让DATAGRID不绑定数据,而只做录入用
- treeview的数据绑定递归
- WinForm TreeView 上逐层加载数据
- TreeView绑定xml数据源
- winform技巧—combobox控件数据绑定
- [转]WinForm数据绑定--BindingContext
- WinForm下CheckedListBox的数据绑定