/// <summary>
/// 获得DataSet 数据集
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public DataSet GetDataSetDatas(string sql)
{
OleDbConnection oleDbconn = null;//连接数据库的对象 SQL SERVER 用
oleDbconn = new OleDbConnection();
string dbConfig = " Provider=sqloledb;Data Source=.;Initial Catalog=MES;User Id=sa;Password=sa; ";
oleDbconn.ConnectionString = dbConfig;
oleDbconn.Open();
DataSet ds = new DataSet();
OleDbDataAdapter odbcda = new OleDbDataAdapter(sql, oleDbconn);
odbcda.Fill(ds);
odbcda.Dispose();
return ds;
}
/// <summary>
/// 绑定数据到TreeView/// </summary>
/// <param name="tvDept"> 需要被绑定TreeView</param>
/// <param name="ParentID">父ID</param>
/// <param name="pNode">TreeNode</param>
public void AddTree(TreeView tvDept ,string ParentID, TreeNode pNode)
{
DataTable tbTree = GetDataSetDatas("SELECT [CodeID],[CodeName],[CodeFatherID] FROM [dbo].[SmallCode]").Tables[0];
DataView dvTree = new DataView(tbTree);
string Fstr = "[CodeFatherID] = '"+ParentID+"' ";
dvTree.RowFilter = Fstr;//查看 CodeFatherID 的值为ParentID的所有行
foreach (DataRowView Row in dvTree)
{
TreeNode Node = new TreeNode();
if (pNode == null) //处理主节点
{
// Node.Name = Row["CodeID"].ToString(); //这里+了2个值分别到Name和Text,可随便
Node.Tag = Row["CodeID"].ToString();
Node.Text = Row["CodeName"].ToString();
tvDept.Nodes.Add(Node); //加入
AddTree(tvDept,Row["CodeID"].ToString(), Node); //递归
}
else //处理子节点
{
Node.Tag = Row["CodeID"].ToString();
Node.Text = Row["CodeName"].ToString();
pNode.Nodes.Add(Node);
AddTree(tvDept, Row["CodeID"].ToString(), Node); //递归
}
}
}
调用时用: ccm.AddTree(tvCodeFatherID, "00", (TreeNode)null); "00"为根节点时的父代码。。。。