这个东西不是什么复杂的东西, 帮着小兄弟写个Demo, 实现个Binding
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
private List<User> GetData()
{
List<User> list = new List<User>();
list.Add(new User() { id = 1, Name = "root", ParentID = 0 });
list.Add(new User() { id = 2, Name = "1", ParentID = 1 });
list.Add(new User() { id = 3, Name = "1.1", ParentID = 2 });
list.Add(new User() { id = 4, Name = "1.2", ParentID = 2 });
list.Add(new User() { id = 5, Name = "2", ParentID = 1 });
list.Add(new User() { id = 6, Name = "2.1", ParentID = 5 });
return list;
}
private void Bind()
{
DataTable dt = new DataTable();
//这个是我获取数据源的代码
dt.Columns.Add("ID");
dt.Columns.Add("Name");
dt.Columns.Add("ParentID");
dt.Columns.Add("URL");
foreach (User item in GetData())
{
DataRow dr = dt.NewRow();
object[] objs = { item.id, item.Name, item.ParentID };
dr.ItemArray = objs;
dt.Rows.Add(dr);
}
DataView dv = dt.DefaultView;
dv.RowFilter = "parentId = 0";
foreach (DataRowView drv in dv)
{
TreeNode RootNode = new TreeNode(drv.Row["Name"].ToString(), drv.Row["Id"].ToString());
BuildNode(dt, RootNode);
this.TreeView1.Nodes.Add(RootNode);
}
}
void BuildNode(DataTable dt, TreeNode node)
{
DataView dv = dt.DefaultView;
dv.RowFilter = "parentId =" + node.Value;
foreach (DataRowView drv in dv)
{
TreeNode ChildNode = new TreeNode(drv.Row["Name"].ToString(), drv.Row["Id"].ToString());
BuildNode(dt, ChildNode);
node.ChildNodes.Add(ChildNode);
}
}
}
public class User
{
public int id { set; get; }
public string Name { set; get; }
public int ParentID { set; get; }
public string URL { set; get; }
}
他们非喜欢DataTable, 米有办法, 墨迹了一点