根结点为固定的 以“跟结点”为例作为text
表结构如下
一级目录 二级目录 三级目录 一级ID 二级ID 三级ID
库1 组1 1# ID ID ID
库1 组1 2# ID ID ID
…………
库1 组2 1# ID ID ID
库1 组2 2# ID ID ID
…………
库2 组1 1# ID ID ID
库2 组1 2# ID ID ID
…………
库2 组2 1# ID ID ID
库2 组2 2# ID ID ID
…………树里面显示名称
单击三级目录时获得一级ID和三级ID
我的思路是逐行取数据先遍历树一级目录
add1()
{
if(一级目录存在)
{
是:遍历子树 ;
当前目录下add2() ;
}
else
{
否:ADD(一级目录);
新建目录下add2();
}
}
add2()
{
if(二级存在)
{
是:当前目录下ADD(三级目录)
}
else
{
否:ADD(二级目录)
新建二级目录下ADD(三级目录)
}
}虽然思路通了,但是水平有限,不能用代码写出来
求代码或者有其他现成方法+代码更好
表结构如下
一级目录 二级目录 三级目录 一级ID 二级ID 三级ID
库1 组1 1# ID ID ID
库1 组1 2# ID ID ID
…………
库1 组2 1# ID ID ID
库1 组2 2# ID ID ID
…………
库2 组1 1# ID ID ID
库2 组1 2# ID ID ID
…………
库2 组2 1# ID ID ID
库2 组2 2# ID ID ID
…………树里面显示名称
单击三级目录时获得一级ID和三级ID
我的思路是逐行取数据先遍历树一级目录
add1()
{
if(一级目录存在)
{
是:遍历子树 ;
当前目录下add2() ;
}
else
{
否:ADD(一级目录);
新建目录下add2();
}
}
add2()
{
if(二级存在)
{
是:当前目录下ADD(三级目录)
}
else
{
否:ADD(二级目录)
新建二级目录下ADD(三级目录)
}
}虽然思路通了,但是水平有限,不能用代码写出来
求代码或者有其他现成方法+代码更好
贴代码
先把我的表的格式写出来
tcan表
ccancode (罐号) coillibrarycode(库号) cname(罐名)tcangroup 表
ccangroupcode (组号) coillibrarycode cname(组名)tcangruopcanmap
ccangroupcode ccancode
我的方法是生成如下的表再递归
ID PID(父编号) 组N 罐N URL
using Microsoft.Web.UI.WebControls;
using MySql.Data.MySqlClient;
namespace youkuzidong
{
/// <summary>
/// left 的摘要说明。
/// </summary>
public class left : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected DataTable treeTable = new DataTable();
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected Microsoft.Web.UI.WebControls.TreeView TreeView2;
protected DataTable tempTable = new DataTable();
private string treestr="";
private void Page_Load(object sender, System.EventArgs e)
{
//this.treeTable = new DataTable();
// 在此处放置用户代码以初始化页面
InitTreeTable();
InitTempTable();
AddToTempTable(); AddGroupToTreeTable();
AddCanToTreeTable(); this.DataGrid1.DataSource = this.treeTable.DefaultView;
this.DataGrid1.DataBind();//检验treeTable是否正确 无其他用处
AddTree(-1, (TreeNode)null);
//Add(-1);
} private void InitTreeTable()
{
DataColumn dc = new DataColumn( "ID" );
dc.DataType = typeof( int );
this.treeTable.Columns.Add( dc ); dc = new DataColumn( "PID" );
dc.DataType = typeof( int );
this.treeTable.Columns.Add( dc ); dc = new DataColumn( "Name" );
dc.DataType = typeof( string );
this.treeTable.Columns.Add( dc ); dc = new DataColumn( "URL" );
dc.DataType = typeof( string );
this.treeTable.Columns.Add( dc );
DataRow dr0 = this.treeTable.NewRow();
DataRow dr1 = this.treeTable.NewRow();
DataRow dr2 = this.treeTable.NewRow(); this.treeTable.Rows.Add( dr0 );
this.treeTable.Rows[ 0 ][ "ID" ] = 0;
this.treeTable.Rows[ 0 ][ "PID" ] = -1;
this.treeTable.Rows[ 0 ][ "Name" ] = "****";
this.treeTable.Rows[ 0 ][ "URL" ] = DBNull.Value; this.treeTable.Rows.Add( dr1 );
this.treeTable.Rows[ 1 ][ "ID" ] = 1;
this.treeTable.Rows[ 1 ][ "PID" ] = 0;
this.treeTable.Rows[ 1 ][ "Name" ] = "****";
this.treeTable.Rows[ 1 ][ "URL" ] = DBNull.Value; this.treeTable.Rows.Add( dr2 );
this.treeTable.Rows[ 2 ][ "ID" ] = 2;
this.treeTable.Rows[ 2 ][ "PID" ] = 0;
this.treeTable.Rows[ 2 ][ "Name" ] = "****";
this.treeTable.Rows[ 2 ][ "URL" ] = DBNull.Value;
}
private void InitTempTable()
{
DataColumn dc = new DataColumn( "库C" );
dc.DataType = typeof( int );
this.tempTable.Columns.Add( dc ); dc = new DataColumn( "罐C" );
dc.DataType = typeof( int );
this.tempTable.Columns.Add( dc ); dc = new DataColumn( "组N" );
dc.DataType = typeof( string );
this.tempTable.Columns.Add( dc ); dc = new DataColumn( "罐N" );
dc.DataType = typeof( string );
this.tempTable.Columns.Add( dc );
}
private void AddToTempTable()
{
MySqlConnection myConn = new MySqlConnection(ConfigurationSettings.AppSettings["ConnString"]);
string strCmd1 = "select COilLibraryCode,CCanCode,CName from tcan";
MySqlDataAdapter da1 = new MySqlDataAdapter(strCmd1,myConn);
DataSet ds = new DataSet();
da1.Fill(ds,"table"); for(int i = 0; i<ds.Tables["table"].Rows.Count; i++)
{
DataRow dr = this.tempTable.NewRow();
this.tempTable.Rows.Add(dr);
this.tempTable.Rows[ i ][ "库C" ] = ds.Tables["table"].Rows[i][0].ToString();
this.tempTable.Rows[ i ][ "罐C" ] = ds.Tables[ "table" ].Rows[i][1].ToString();
this.tempTable.Rows[ i ][ "罐N" ] = ds.Tables[ "table" ].Rows[i][2].ToString(); string strCmd2 = "select CCanGroupCode from tcangroupcanmap where CCanCode = " + this.tempTable.Rows[ i ][ "罐C" ].ToString();
MySqlDataAdapter da2 = new MySqlDataAdapter(strCmd2,myConn);
da2.Fill(ds,"temp1"); string strCmd3 = "select CName from tcangroup where CCanGroupCode = " + ds.Tables["temp1"].Rows[i][0].ToString();
MySqlDataAdapter da3 = new MySqlDataAdapter(strCmd3,myConn);
da3.Fill(ds,"temp2");
string str = ds.Tables["temp2"].Rows[i][0].ToString();
this.tempTable.Rows[ i ][ "组N" ] = ds.Tables["temp2"].Rows[i][0].ToString();
}
}
private void AddGroupToTreeTable()
{
MySqlConnection myConn = new MySqlConnection(ConfigurationSettings.AppSettings["ConnString"]);
string strCmd = "select tcangroup.COilLibraryCode,tcangroup.CName from tcangroup";// where tcangroup.COilLibrayCode=toillibrary.COilLibrayCode and toillibrary.CRegionCode=1";
MySqlDataAdapter da = new MySqlDataAdapter(strCmd,myConn);
DataSet ds = new DataSet();
da.Fill(ds,"group");
for(int i = 0; i<ds.Tables["group"].Rows.Count; i++)
{
DataRow dr = this.treeTable.NewRow();
this.treeTable.Rows.Add(dr);
int temp = this.treeTable.Rows.Count;
this.treeTable.Rows[ i+3 ][ "ID" ] = i+3;
this.treeTable.Rows[ i+3 ][ "PID" ] = ds.Tables[ "group" ].Rows[i][0].ToString();
this.treeTable.Rows[ i+3 ][ "Name" ] = ds.Tables[ "group" ].Rows[i][1].ToString();
this.treeTable.Rows[ i+3 ][ "URL" ] = DBNull.Value;
} }
private void AddCanToTreeTable()
{
for(int i = 0; i<this.tempTable.Rows.Count; i++)
{
int treeTableRows = this.treeTable.Rows.Count;
for(int j = 0; j<this.treeTable.Rows.Count; j++)
{
string t1 = this.tempTable.Rows[i][0].ToString();
string t2 = this.treeTable.Rows[j][1].ToString();
string t3 = this.tempTable.Rows[i][2].ToString();
string t4 = this.treeTable.Rows[j][2].ToString();
if(t1==t2&&t3==t4)
//if(this.tempTable.Rows[i][0]==this.treeTable.Rows[j][1]&&this.tempTable.Rows[i][2]==this.treeTable.Rows[j][2])
{
DataRow dr = this.treeTable.NewRow();
this.treeTable.Rows.Add(dr);
int temp = this.treeTable.Rows.Count - 1;
this.treeTable.Rows[ treeTableRows ][ "ID" ] = treeTableRows;
this.treeTable.Rows[ treeTableRows ][ "PID" ] = this.treeTable.Rows[j][0].ToString();
this.treeTable.Rows[ treeTableRows ][ "Name" ] = this.tempTable.Rows[i][3].ToString();
this.treeTable.Rows[ treeTableRows ][ "URL" ] = "./default.aspx"; break;
}
}
}
}
public void AddTree(int PID,TreeNode pNode)
{
DataView dvTree = new DataView(this.treeTable);
dvTree.RowFilter = "[PID] = " + PID; foreach(DataRowView Row in dvTree)
{
TreeNode Node=new TreeNode() ;
if(pNode == null)
{
//添加根结点
Node.Text = Row["Name"].ToString();
TreeView2.Nodes.Add(Node);
Node.Expanded=true;
Node.NavigateUrl = Row["URL"].ToString();
Node.Target = "right";
AddTree(Int32.Parse(Row["ID"].ToString()), Node);
}
else
{
//添加当前结点子结点
Node.Text = Row["Name"].ToString();
pNode.Nodes.Add(Node);
Node.Expanded = true;
Node.NavigateUrl = Row["URL"].ToString();
Node.Target = "right";
AddTree(Int32.Parse(Row["ID"].ToString()),Node);
}
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
}
}方法是笨,不过实现了总是好事
慢慢再想怎么才能改进
你这样写有点死了,网上很多示例,go search!