//myTree.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsApplication1
{
public partial class myTree : Form
{
private OleDbDataAdapter oda;
private DataSet ds;
baseClass.OleDbClass oledbclass = new WindowsApplication1.baseClass.OleDbClass();
private DataTable treeTab = new DataTable();
public myTree()
{
InitializeComponent();
}
private void myTree_Load(object sender, EventArgs e)
{
this.oda = oledbclass.OleDa("select * from tb_dept order by deptId");
this.oda.Fill(treeTab);
DataTable temptab = treeTab.Copy();
DataView myView = new DataView(temptab);
myView.RowFilter = "ParentIndex=-1";
if (myView.Count > 0)
{
foreach (DataRowView myRow in myView)
{
string strDeptName = myRow["DeptName"].ToString().Trim();
this.treevDept.Nodes.Add(new TreeNode(strDeptName));
myTreeView(strDeptName, treevDept.Nodes[0], myRow);
treevDept.SelectedNode = treevDept.Nodes[0];
}
}
}
private void myTreeView(string parentPart, TreeNode parentNode, DataRowView parentRow)
{
string strDeptNode = "";
DataTable temptab = treeTab.Copy();
DataView myView = new DataView(treeTab);
myView.RowFilter = "ParentIndex='" + parentRow["deptId"].ToString().Trim() + "'";
foreach (DataRowView myRow in myView)
{
string strDeptName = myRow["DeptName"].ToString().Trim();
TreeNode myNode = new TreeNode(strDeptName);
parentNode.Nodes.Add(myNode);
myTreeView(strDeptName, myNode, myRow);
}
}
//选中部门
private void treevDept_AfterSelect(object sender, TreeViewEventArgs e)
{
this.txtDeptName.Text = e.Node.Text.Trim();
ds = oledbclass.OleDs("select * from tb_dept where deptName='" + this.txtDeptName.Text + "'", "tb_dept");
DataRowView rowview=ds.Tables["tb_dept"].DefaultView[0];
this.txtDeptDirector.Text = rowview["deptDirector"].ToString();
this.txtDeptId.Text = rowview["deptId"].ToString();
this.txtDeptPhone.Text = rowview["deptPhone"].ToString();
this.txtParentIndex.Text = rowview["ParentIndex"].ToString();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
namespace WindowsApplication1.baseClass
{
class OleDbClass
{
private OleDbConnection conn;
private OleDbDataAdapter oda;
private DataSet ds;
private OleDbCommand ocmd;
public OleDbConnection oleDbConn()
{
conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Jet OLEDB:Database Password=00000;Data Source=" + @"|datadirectory|\hr.mdb");
return conn;
}
public DataSet OleDs(string strsql,string strtab)
{
conn = this.oleDbConn();
oda = new OleDbDataAdapter(strsql,conn);
ds = new DataSet();
oda.Fill(ds, strtab);
conn.Close();
return ds;
}
public OleDbDataAdapter OleDa(string strsql)
{
conn = this.oleDbConn();
oda = new OleDbDataAdapter(strsql,conn);
return oda;
}
public void OleCmd(string strsql)
{
conn = this.oleDbConn();
conn.Open();
ocmd = new OleDbCommand(strsql,conn);
ocmd.ExecuteNonQuery();
conn.Close();
}
}
}