c# treeview控件动态绑定数据库,实现2级目录,当输入同一父节点的子节点时,在相同父节点下添加子节点

希望能给出相关代码例子,谢谢... 希望能给出相关代码例子,谢谢 展开
 我来答
传奇勇者
2010-10-27 · TA获得超过747个赞
知道大有可为答主
回答量:1706
采纳率:75%
帮助的人:800万
展开全部
我这儿这个是无限级动态加载的,有些复杂,慢慢看
private void LoadData()
{
try
{
//得到所有所有父节点,放到DataTable中,这里默认根节点的父节为0
BindRootNode(InitRootNodeDataTable(), treeCatalog.Nodes); //绑定所有的父节点
}
catch (Exception ex)
{
UDS.Components.Error.Log(ex.ToString());
}
}

#region 得到父节点的字节点,放到DataTable中
/// <summary>
/// 初始化 RootNode DataTable
/// </summary>
private DataTable InitRootNodeDataTable()
{
Database data = new Database();
SqlDataReader dataReader = null;

try
{
String classSQL = "SELECT *,1 as childNum FROM UDS_Class WHERE ClassID = " + topID.ToString();
dataReader = new Database().ExSQLReDr(classSQL);
}
catch (Exception ex)
{
UDS.Components.Error.Log(ex.ToString());
}
DataTable t = Tools.ConvertDataReaderToDataTable(dataReader);
dataReader.Close();
t.TableName = "TreeView";
return t;
}

/// <summary>
/// 初始化 ChildNode DataTable
/// </summary>
private DataTable GetChildNodeDataTable(int ClassParentID)
{
Database data = new Database();
SqlDataReader dataReader = null;
string sql = "select *, (select count(*) from uds_class where Display < 1 and classParentID = a.ClassID and classParentid <> ClassID) as childNum from uds_class a where Display < 1 and classParentid =" + ClassParentID + " and classParentid <> ClassID order by DocSort";
try
{
dataReader = data.ExSQLReDr(sql); ;
}
catch (Exception ex)
{
Response.Write(ex.ToString());
UDS.Components.Error.Log(ex.ToString());
}
DataTable t = Tools.ConvertDataReaderToDataTable(dataReader);
dataReader.Close();
t.TableName = "TreeView";
return t;
}
#endregion

#region 填充节点
private void BindRootNode(DataTable table, TreeNodeCollection node)
{
DataView dv = new DataView(table);
foreach (DataRowView drv in dv)
{
TreeNode NewNode = new TreeNode();
NewNode.Value = drv["ClassID"].ToString();
NewNode.Text = "<span onmousemove=javascript:title='" + drv["ClassRemark"].ToString() + "'>" + drv["ClassName"].ToString() + "</span>";
NewNode.ImageUrl = GetIcon(drv["ClassType"].ToString());
NewNode.NavigateUrl = "Default.aspx?topID=" + NewNode.Value;
NewNode.Target = "_top";
node.Add(NewNode);

//判断是否还有子节点,如果还有子节点,显示展开符号
if (Convert.ToInt32(drv["childNum"]) > 0)
NewNode.PopulateOnDemand = true;
}
}

private void BindNode(DataTable table, TreeNodeCollection node)
{
DataView dv = new DataView(table);
foreach (DataRowView drv in dv)
{
TreeNode NewNode = new TreeNode();
NewNode.Value = drv["ClassID"].ToString();
NewNode.Text = "<span onmousemove=javascript:title='" + drv["ClassRemark"].ToString() + "'>" + drv["ClassName"].ToString() + "</span>";
switch (drv["ClassType"].ToString())
{
case "0":
NewNode.NavigateUrl = "Default.aspx?topID=" + NewNode.Value;
NewNode.Target = "_top";
break;
case "1":
NewNode.NavigateUrl = "Document/listview.aspx?topID=" + topID.ToString() + "&ClassID=" + NewNode.Value;
NewNode.Target = "MainFrame";
break;
}
node.Add(NewNode);

//判断是否还有子节点,如果还有子节点,显示展开符号
if (Convert.ToInt32(drv["childNum"]) > 0)
NewNode.PopulateOnDemand = true;
}
}

#region 获取节点图标
/// <summary>
/// 获取节点图标
/// </summary>
private string GetIcon(string ClassType)
{
string rtnValue = "DataImages/";
switch (ClassType)
{
case "0":
rtnValue += "flag.gif";
break;
case "1":
rtnValue += "myDoc.gif";
break;
case "2":
rtnValue += "mail.gif";
break;
case "3":
rtnValue += "page.gif";
break;
case "4":
rtnValue += "staff.gif";
break;
case "5":
rtnValue += "help_page.gif";
break;
case "6":
rtnValue += "MyTask.gif";
break;
case "7":
rtnValue += "mail.gif";
break;
case "8":
rtnValue += "myDoc.gif";
break;
case "9":
rtnValue += "DocFlow.gif";
break;
case "10":
rtnValue += "ClientManage.gif";
break;
case "11":
rtnValue += "myLinkman.gif";
break;
case "12":
rtnValue += "position.gif";
break;
case "13":
rtnValue += "roleManage.gif";
break;
case "14":
rtnValue += "kaoqin.gif";
break;
case "15":
rtnValue += "workadmin.gif";
break;
case "16":
rtnValue += "message.gif";
break;
case "17":
rtnValue += "usbkey.gif";
break;
default:
rtnValue += "red_ball.gif";
break;
}
return rtnValue;
}
#endregion

#endregion

#region 填充节点事件
protected void TreeCatalog_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
getDataNode(e.Node.Value, e.Node); //点加号展开时调用.得到数据并绑定.传入点击结点 ID,和点击节点对像.
}
#endregion

private void getDataNode(String ParentId, TreeNode Node)
{
BindNode(GetChildNodeDataTable(Convert.ToInt16(ParentId)), Node.ChildNodes); //向结点填充数据.
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式