日期:2014-05-19  浏览次数:20397 次

计算节点深度
表字段
id   parentid   title
绑定时如下:
private   void   BindData(DataTable   dt,int   id)
{
DataView   dv   =   new   DataView(dt);
dv.RowFilter   =   "t_parentid   =   "   +   id.ToString();
foreach(DataRowView   drv   in   dv)
{
string   blank= " ";
if(id!=0)
{
for(int   i=0;i <GetDepth();i++)
{
blank+= "- ";
}
}
ListItem   li=new   ListItem();
li.Text=blank+drv[ "t_id "].ToString()+drv[ "t_title "].ToString();
li.Value=drv[ "t_id "].ToString();
this.ListBox1.Items.Add(li);
BindData(dt,Convert.ToInt32(drv[ "t_id "]));
}    
}


GetDepth();这个方法应该怎写呀。


------解决方案--------------------

换个思路,不需要计算深度 开始调用BindData(dt,id0,0)

private void BindData(DataTable dt,int id,int deep)
{
DataView dv = new DataView(dt);
dv.RowFilter = "t_parentid = " + id.ToString();
foreach(DataRowView drv in dv)
{
string blank= " ";
if(id!=0)
{
for(int i=0;i <deep;i++)
{
blank+= "- ";
}
}
ListItem li=new ListItem();
li.Text=blank+drv[ "t_id "].ToString()+drv[ "t_title "].ToString();
li.Value=drv[ "t_id "].ToString();
this.ListBox1.Items.Add(li);
BindData(dt,Convert.ToInt32(drv[ "t_id "]),deep+1);
}
}

------解决方案--------------------
//试试
private int iDepth = 0; //递归
private int myDepth = 0; //外围
private void BindData(DataTable dt, int id)
{
DataView dv = new DataView(dt);
dv.RowFilter = "t_parentid = " + id.ToString();

foreach (DataRowView drv in dv)
{
string blank = " ";
if (id != 0)
{
iDepth++;
for (int i = 0; i < iDepth; i++)
{
blank += "- ";
}
}
ListItem li = new ListItem();
li.Text = blank + drv[ "t_id "].ToString() + drv[ "t_title "].ToString();
li.Value = drv[ "t_id "].ToString();
this.ListBox1.Items.Add(li);
BindData(dt, Convert.ToInt32(drv[ "t_id "]));
myDepth++;
iDepth = myDepth;
}
}