C1TreeView后台绑定DataTable,递归
问题描述:C1TreeView后台递归绑定问题解答:直接上代码
前台代码(直接拖入一个C1TreeView控件)
<wijmo:C1TreeView ID="C1TreeView1" runat="server" ShowCheckBoxes="true">
</wijmo:C1TreeView>
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using C1.Web.Wijmo.Controls.C1TreeView;
public partial class tree : System.Web.UI.Page
{
DataTable areaDT = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
setDT();
C1TreeViewNode node = new C1TreeViewNode();
DataRow[] far = areaDT.Select("subid=0");
DataRow drfar = far;
node.Text = drfar.ToString();
this.C1TreeView1.Nodes.Add(node);
DataRow[] drarea = areaDT.Select("subid=1");
for (int i = 0; i < drarea.Length; i++)
{
AddArea(this.C1TreeView1.Nodes.Nodes, drarea);
}
}
private void AddArea(C1TreeViewNodeCollection tvn, DataRow dr)
{
int areaid = Convert.ToInt32(dr);
string areaname = dr.ToString().Trim();
C1TreeViewNode newnode = new C1TreeViewNode();
newnode.Text = areaname;
tvn.Add(newnode);
C1TreeViewNodeCollection tvn1 = newnode.Nodes;
DataRow[] drssuperarea = areaDT.Select("subid=" + areaid);
for (int i = 0; i < drssuperarea.Length; i++)
{
AddArea(tvn1, drssuperarea);
}
}
private void setDT()
{
areaDT.Columns.Add("id");
areaDT.Columns.Add("name");
areaDT.Columns.Add("subid");
DataRow dr = areaDT.NewRow();
dr["id"] = 1;
dr["name"] = "父文件";
dr["subid"] = 0;
areaDT.Rows.Add(dr);
dr = areaDT.NewRow();
dr["id"] = 2;
dr["name"] = "子文件1";
dr["subid"] = 1;
areaDT.Rows.Add(dr);
dr = areaDT.NewRow();
dr["id"] = 3;
dr["name"] = "孙文件";
dr["subid"] = 2;
areaDT.Rows.Add(dr);
}
}
效果截图
回复 1楼rijun的帖子
非常感谢您提供的源代码,为需要解决此问题的用户提供帮助。
已经发送金币奖励。 就有一点小BUG,你看看
DataRow[] drarea = areaDT.Select("subid=1");
for (int i = 0; i < drarea.Length; i++)
{
setAddArea(this.C1TreeView1.Nodes.Nodes, drarea);
}
}
private void setAddArea(C1TreeViewNodeCollection tvn, DataRow dr)
{
int areaid = Convert.ToInt32(dr);
string areaname = dr.ToString().Trim();
C1TreeViewNode newnode = new C1TreeViewNode();
newnode.Text = areaname;
tvn.Add(newnode);
C1TreeViewNodeCollection tvn1 = newnode.Nodes;
DataRow[] drssuperarea = areaDT.Select("subid=" + areaid);
for (int i = 0; i < drssuperarea.Length; i++)
{
setAddArea(tvn1, drssuperarea);
}
} 就有一点小BUG,你看看
DataRow[] drarea = areaDT.Select("subid=1");
for (int i = 0; i < drarea.Length; i++)
{
setAddArea(this.C1TreeView1.Nodes.Nodes, drarea);
}
}
private void setAddArea(C1TreeViewNodeCollection tvn, DataRow dr)
{
int areaid = Convert.ToInt32(dr);
string areaname = dr.ToString().Trim();
C1TreeViewNode newnode = new C1TreeViewNode();
newnode.Text = areaname;
tvn.Add(newnode);
C1TreeViewNodeCollection tvn1 = newnode.Nodes;
DataRow[] drssuperarea = areaDT.Select("subid=" + areaid);
for (int i = 0; i < drssuperarea.Length; i++)
{
setAddArea(tvn1, drssuperarea);
}
} 如果有三层的会出问题,给你修改好了,测试表如下。
private void setDT()
{
areaDT.Columns.Add("id");
areaDT.Columns.Add("name");
areaDT.Columns.Add("subid");
DataRow dr = areaDT.NewRow();
dr["id"] = 1;
dr["name"] = "父文件";
dr["subid"] = 0;
areaDT.Rows.Add(dr);
dr = areaDT.NewRow();
dr["id"] = 2;
dr["name"] = "子文件3";
dr["subid"] = 1;
areaDT.Rows.Add(dr);
dr = areaDT.NewRow();
dr["id"] = 5;
dr["name"] = "子文件2";
dr["subid"] = 1;
areaDT.Rows.Add(dr);
dr = areaDT.NewRow();
dr["id"] = 6;
dr["name"] = "子文件1";
dr["subid"] = 1;
areaDT.Rows.Add(dr);
dr = areaDT.NewRow();
dr["id"] = 3;
dr["name"] = "孙文件";
dr["subid"] = 2;
areaDT.Rows.Add(dr);
dr = areaDT.NewRow();
dr["id"] = 4;
dr["name"] = "孙文件1";
dr["subid"] = 2;
areaDT.Rows.Add(dr);
}
yingri 发表于 2016-10-17 10:40
如果有三层的会出问题,给你修改好了,测试表如下。
private void setDT()
{
谢谢您的反馈和对产品的支持。
特赠送金币,以资鼓励
页:
[1]