关于TREEVIEW 绑定数据设置节点,在线等,急!谢谢!
表
BigClassID(标识列)
PBigClassID(判断是否根接点要根据这列,为0就是根点)
BigClassName(TREEVIEW要绑定的字段)
代码如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateTreeView();
}
}
private void PopulateTreeView()
{
DataTable treeViewData = GetTreeViewData();
AddTopTreeViewNodes(treeViewData);
}
private DataTable GetTreeViewData()
{
string cmd = "select BigClassID,PBigClassID,BigClassName from BigClassStru";
string cn = "server=192.168.1.179;database=sky3;uid=sa;pwd=hp1502...";
SqlDataAdapter dad = new SqlDataAdapter(cmd,cn);
DataTable dtblBigClassStru = new DataTable();
dad.Fill(dtblBigClassStru);
return dtblBigClassStru;
}
private void AddTopTreeViewNodes(DataTable treeViewData)
{
DataView view = new DataView(treeViewData);
view.RowFilter = "PBigClassID is 0";
foreach (DataView row in view)
{
TreeNode newNode = new TreeNode(row["BigClassName"].ToString(),row["PBigClassID"].ToString());
TreeView1.Nodes.Add(newNode);
AddChildTreeViewNodes(treeViewData,newNode);
}
}
private void AddChildTreeViewNodes(DataTable treeViewData,TreeNode parentTreeViewNode)
{
DataView view = new DataView(treeViewData);
view.RowFilter = "PBigClassID=" + parentTreeViewNode.Value;
foreach (DataRowView row in view)
{
TreeNode newNode = new TreeNode(row["BigClassName"].ToString(),row["BigClassID"].ToString());
parentTreeViewNode.ChildNodes.Add(newNode);
AddChildTreeViewNodes(treeViewData, newNode);
}
}
------解决方案--------------------
TreeNode newNode = new TreeNode(row[0].ToString(),row[1].ToString());