public void treeviewView()
{
//显示父节点
string sql = "select * from Company_Infor";
SqlDataAdapter adapter = new SqlDataAdapter(sql, conn.Link());
DataSet resultDS = new DataSet();
adapter.Fill(resultDS, "Company_Infor");
foreach (DataRow row in resultDS.Tables["Company_Infor"].Rows)
{
sql = "select top 1 * from Company_Infor";
adapter = new SqlDataAdapter(sql, conn.Link());
adapter.Fill(resultDS, row["Company_Name"].ToString());
TreeNode tn = new TreeNode();
tn.Name = row["Company_ID"].ToString();
tn.Text = row["Company_Name"].ToString();
string companyid = row["Company_ID"].ToString();
treeView1.Nodes.Add(tn);
//部门子节点
string sectionsql = "select * from Section_Infor";
SqlDataAdapter sectionadapter = new SqlDataAdapter(sectionsql, conn.Link());
DataSet sectionDS = new DataSet();
sectionadapter.Fill(sectionDS, "Section_Infor");
foreach (DataRow sectionrow in sectionDS.Tables["Section_Infor"].Rows)
{
sectionsql = "select top 1 * from Section_Infor";
sectionadapter = new SqlDataAdapter(sectionsql, conn.Link());
sectionadapter.Fill(sectionDS, sectionrow["Section_Name"].ToString());
TreeNode sectn = new TreeNode();
sectn.Name = sectionrow["Section_ID"].ToString();
sectn.Text = sectionrow["Section_Name"].ToString();
string FKcompanyid = sectionrow["Company_ID"].ToString();
if (FKcompanyid == companyid)
tn.Nodes.Add(sectn);
}
}
}
这个也许你有用
是加载的数据库中的字段
但代码你还要封装哈