Csharp: Treeview check list value
1 /// <summary> 2 /// 選擇的節點 3 /// 塗聚文 20121116 4 /// 捷為工作室 5 /// 6 /// </summary> 7 /// <param name="sender"></param> 8 /// <param name="e"></param> 9 private void treeView1_AfterCheck(object sender, TreeViewEventArgs e) 10 { 11 #region 4 12 13 if (e.Action != TreeViewAction.Unknown) 14 { 15 16 //Event call by mouse or key-press 17 18 SetNodeCheckStatus(e.Node, e.Node.Checked); 19 20 } 21 #endregion 22 23 24 } 25 /// <summary> 26 /// 選擇的節點 27 /// </summary> 28 /// <param name="tn"></param> 29 /// <param name="Checked"></param> 30 private void SetNodeCheckStatus(TreeNode tn, bool Checked) 31 { 32 33 if (tn == null) return; 34 35 // Check children nodes 36 37 foreach (TreeNode tnChild in tn.Nodes) 38 { 39 40 tnChild.Checked = Checked; 41 42 SetNodeCheckStatus(tnChild, Checked); 43 44 } 45 46 // Set parent check status 47 48 TreeNode tnParent = tn; 49 50 int nNodeCount = 0; 51 52 while (tnParent.Parent != null) 53 { 54 55 tnParent = (TreeNode)(tnParent.Parent); 56 57 nNodeCount = 0; 58 59 foreach (TreeNode tnTemp in tnParent.Nodes) 60 61 if (tnTemp.Checked == Checked) 62 63 nNodeCount++; 64 65 if (nNodeCount == tnParent.Nodes.Count) 66 67 tnParent.Checked = Checked; 68 69 else 70 71 tnParent.Checked = false; 72 73 } 74 75 } 76 77 78 #region 選擇的節點 79 /// <summary> 80 /// http://www.codeproject.com/Questions/196856/C-NET-Treeview-Node-Checked 81 /// http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.aftercheck.aspx 82 /// </summary> 83 /// <param name="treeNode"></param> 84 /// <param name="nodeChecked"></param> 85 public void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked) 86 { 87 foreach (TreeNode node in treeNode.Nodes) 88 { 89 node.Checked = nodeChecked; 90 if (node.Nodes.Count > 0) 91 { 92 this.CheckAllChildNodes(node, nodeChecked); 93 } 94 } 95 } 96 #endregion 97 /// <summary> 98 /// 顯示選擇的節點 99 /// </summary> 100 /// <param name="sender"></param> 101 /// <param name="e"></param> 102 private void button1_Click(object sender, EventArgs e) 103 { 104 105 DataTable dt = new DataTable(); 106 dt.Columns.Add("Uid", typeof(Guid)); 107 dt.Columns.Add("ID", typeof(int)); 108 dt.Columns.Add("Name", typeof(string)); 109 dt.Columns.Add("ParentID", typeof(int)); 110 111 List<TreeNode> selectedNodes = new List<TreeNode>(); 112 GetSelectedTreeNode(treeView1.Nodes, selectedNodes); 113 int i = 0; 114 foreach (TreeNode node in selectedNodes) 115 { 116 if (!string.IsNullOrEmpty(node.ToolTipText)) 117 { 118 dt.Rows.Add(new Guid(node.ToolTipText), node.Name, node.Text,node.Parent.Name); 119 } 120 i++; 121 } 122 123 124 dataGridView1.DataSource = dt; 125 126 127 128 129 130 } 131 /// <summary> 132 /// 選擇的節點的值 133 /// </summary> 134 /// <param name="nodes"></param> 135 /// <param name="selectedNodes"></param> 136 void GetSelectedTreeNode(TreeNodeCollection nodes, List<TreeNode> selectedNodes) 137 { 138 foreach (TreeNode node in nodes) 139 { 140 if (node.Checked) 141 { 142 selectedNodes.Add(node); 143 } 144 GetSelectedTreeNode(node.Nodes, selectedNodes); 145 } 146 }
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)