日期:2014-05-18  浏览次数:20480 次

求解关于TreeView如何操作三张表的问题
每个类别对应多个系列     每个系列对应多个产品  
类别表     LBID(自增ID)   LBMC(类别名称)
系列表     XLID(自增ID)   LBID(与类别表关联)   XLMC(系列名称)
产品表     CPID(自增ID)   LBID     XLID   CPMC(产品名称)   CPJG(价格)等等    
请问如何利用TreeView控件实现
-类别1
        -系列1
            -产品1
            -产品2
      +系列2
      .
      .
      .
+类别2
+类别3
    .
    .
    .
如此的树状图呢?看了论坛上不少TreeView的问题,解决这样问题的很少,希望高手能帮忙

------解决方案--------------------
递归一下
------解决方案--------------------
public string strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}aa.mdb;Jet OLEDB:Database Password=123 ";
public string strConnection = " ";
public Form1()
{
InitializeComponent();
strConnection = string.Format(strConnectionString, Application.StartupPath + "\\ ");
InitTree();
}
public void InitTree()
{
string strSQL = "SELECT cp.cpid,cp.cpmc,lb.lbmc,xl.xlmc,lb.lbid,xl.xlid FROM CP,lb,xl where cp.lbid=lb.lbid and xl.xlid=cp.xlid ";

OleDbConnection objConnection = new OleDbConnection(strConnection);
objConnection.Open();
OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQL, objConnection);
DataSet ds = new DataSet();
objAdapter.Fill(ds);
objAdapter.Dispose();
objConnection.Close();
objConnection.Dispose();

DataTable dt = new DataTable();
dt = ds.Tables[0].Copy();

InitTreeNode(treeView1.Nodes, " ", ref dt, 0);

}

public void InitTreeNode(TreeNodeCollection tnc, string strPID, ref DataTable dt,int iPath)
{

string strRunTempID = " ";
string strTempId = " ";
if (iPath == 0)
{
//进行第一层遍历
foreach (DataRow dr in dt.Rows)
{
strRunTempID = dr[ "lbid "].ToString().Trim();
if (strTempId == strRunTempID && strTempId != " ")
{
continue;
}
else
{
string strXLID = dr[ "lbid "].ToString().Trim();

strTempId = strRunTempID;
TreeNode tn = new TreeNode();
tn.Text = dr[ "lbmc "].ToString().Trim();
tnc.Add(tn);
InitTreeNode(tn.Nodes, strXLID, ref dt, 1);
}

}
}

if (iPath == 1)
{
strRunTempID = " ";
strTempId = " ";
//进行第2层遍历
DataView dv = new DataView();
dv.Table = dt;
dv.RowFilter = "lbid = ' " + strPID + " ' "