本帖最后由 舞柯庶 于 2015-7-3 14:00 编辑
public void DDB_List(DropDownBox DDB_ID, string sql, string TopParentValue)
{
Tree xTree = new Tree();
xTree.Hidden = true;
xTree.ShowHeader = false;
xTree.EnableMultiSelect = DDB_ID.EnableMultiSelect; //和DropDownBox 保持一致
xTree.KeepCurrentSelection = DDB_ID.EnableMultiSelect; //保持选中状态
//构造Tree 树结构
(new TreeInfo()).LoadData(xTree, sql, TopParentValue, "", false);
DDB_ID.PopPanel.Add(xTree);
}
//TreeInfo.cs
#region 构造结构菜单树
/// <summary>
/// Tree模块树
/// Sql 格式 Select A T_Value,B T_Text,'父代码' T_ParentCode,'是否启用' IsEnable From Table
/// </summary>
public class TreeInfo
{
//构造结构菜单树 IsShow=是否启用
//Sql 格式 Select A T_Value,B T_Text,'父代码' T_ParentCode,'是否启用' IsEnable From Table
public void LoadData(Tree xTree1, string Sql, string TopParentValue, string ExtraField, Boolean xCheck)
{
xTree1.Nodes.Clear();
DB db = new DB();
// 模拟从数据库返回数据表
DataTable table = db.Dt_table(Sql);
DataSet ds = new DataSet();
ds.Tables.Add(table);
ds.Relations.Add("TreeRelation", ds.Tables[0].Columns["T_Value"], ds.Tables[0].Columns["T_ParentCode"], false);
foreach (DataRow row in ds.Tables[0].Rows)
{
if (row["T_ParentCode"].ToString() == TopParentValue)
{
TreeNode node = new TreeNode();
if (ExtraField != "")
{
node.Text = row["T_Text"].ToString() + "【" + row[ExtraField].ToString() + "】";
}
else
{
node.Text = row["T_Text"].ToString();
}
node.NodeID = row["T_Value"].ToString(); //node.Icon = Icon.RecordGreen;
node.Expanded = true; //是否展开
//node.EnableClickEvent = true;
if (row["IsEnable"].ToString() == "1")
{
node.Selectable = true;
}
else
{
node.Selectable = false; ; //禁用
}
if (xCheck == true)
{
node.EnableCheckBox = true;
node.EnableClickEvent = true; //升级注释 替换 node.EnablePostBack = true;
node.EnableCheckEvent = true; //升级注释 替换 node.AutoPostBack = true;
}
xTree1.Nodes.Add(node);
ResolveSubTree(row, node, ExtraField, xCheck);
}
}
}
//循环树节点 xIsEnable=1 禁用
private void ResolveSubTree(DataRow dataRow, TreeNode treeNode, string ExtraField, Boolean xCheck)
{
DataRow[] rows = dataRow.GetChildRows("TreeRelation");
if (rows.Length > 0)
{
foreach (DataRow row in rows)
{
TreeNode node = new TreeNode();
if (ExtraField != "")
{
node.Text = row["T_Text"].ToString() + "【" + row[ExtraField].ToString() + "】";
}
else
{
node.Text = row["T_Text"].ToString();
}
node.NodeID = row["T_Value"].ToString();
//node.ToolTip = row["T_Value"].ToString();
//node.Icon = Icon.RecordGreen;
if (row["IsEnable"].ToString() == "1")
{
node.Selectable = true;
}
else
{
node.Selectable = false; ; //禁用
}
if (xCheck == true)
{
node.EnableCheckBox = true;
node.EnableClickEvent = true; //升级注释 替换 node.EnablePostBack = true;
node.EnableCheckEvent = true; //升级注释 替换 node.AutoPostBack = true;
}
treeNode.Nodes.Add(node);
ResolveSubTree(row, node, ExtraField, xCheck); //循环调用
}
}
}
}
#endregion
|