#region 绑定树 private void BindTree(DataTable dt, TreeView tv,int type) { //先清空树节点 tv.Nodes.Clear(); //创建根节点 TreeNode root = new TreeNode("12","1212");
DataTable tmpdt = new DataTable(); string platformIds = string.Empty; tmpdt = DBOperate.GetDataTable("select PlatformId from SupplierPlatformRelation where type=" + type + ""); foreach (DataRow item in tmpdt.DataSet.Tables[0].Rows) { if (string.IsNullOrEmpty(platformIds)) { platformIds = item[0].ToString(); } else { platformIds += "," + item[0].ToString(); } }
foreach (DataRow item in dt.DataSet.Tables[0].Rows) { TreeNode tn = new TreeNode(); tn.Text = item[1].ToString(); tn.Value = item[0].ToString(); if (string.IsNullOrEmpty(item[2].ToString())) { tn.ToolTip = (bool)item[4] == true ? "1" : "0"; if (!string.IsNullOrEmpty(platformIds)) { string[] tmps = platformIds.Split(','); bool isExist = false; for (int i = 0; i < tmps.Length; i++) { if (tmps[i].ToString() == item[0].ToString()) { isExist = true; break; } } if (isExist) tn.Checked = true; }
//root.ChildNodes.Add(tn); tv.Nodes.Add(tn); } else { tn.ToolTip = "1"; foreach (TreeNode node in tv.Nodes) { AddNodes(node, tn, item[2].ToString(), (bool)item[4], platformIds); } } }
//tv.Nodes.Add(root);
tv.ExpandAll(); }
/// <summary> /// 添加节点 /// </summary> /// <param name="tn">根节点</param> /// <param name="newNode">需要添加的节点</param> /// <param name="parentId">父节点ID</param> /// <param name="isEnabled">是否有效</param> /// <param name="platformIds">平台集合</param> private void AddNodes(TreeNode tn, TreeNode newNode, string parentId, bool isEnabled, string platformIds) { if (tn.Value != null && tn.Value.ToString() == parentId && isEnabled) { if (!string.IsNullOrEmpty(platformIds)) { string[] tmps = platformIds.Split(','); bool isExist = false; for (int i = 0; i < tmps.Length; i++) { if (tmps[i].ToString() == newNode.Value.ToString()) { isExist = true; break; } } if (isExist) newNode.Checked = true; } tn.ChildNodes.Add(newNode); } else { for (int i = 0; i < tn.ChildNodes.Count; i++) { if (parentId == tn.ChildNodes[i].Value.ToString() && isEnabled) { if (!string.IsNullOrEmpty(platformIds)) { string[] tmps = platformIds.Split(','); bool isExist = false; for (int m = 0; m < tmps.Length; m++) { if (tmps[m].ToString() == newNode.Value.ToString()) { isExist = true; break; } } if (isExist) newNode.Checked = true; } tn.ChildNodes[i].ChildNodes.Add(newNode); } else { AddNodes(tn.ChildNodes[i], newNode, parentId, isEnabled, platformIds); } } } } #endregion
|