Csharp: Treeview check list value

/// <summary>
         /// 選擇的節點
         /// 塗聚文 20121116
         /// 捷為工作室
         ///
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
         {
  #region  4

             if (e.Action != TreeViewAction.Unknown)
             {

                 //Event call by mouse or key-press

                 SetNodeCheckStatus(e.Node, e.Node.Checked);

             }
             #endregion 

         }
         /// <summary>
         /// 選擇的節點
         /// </summary>
         /// <param name="tn"></param>
         /// <param name="Checked"></param>
         private void SetNodeCheckStatus(TreeNode tn, bool Checked)
         {

             if (tn == null) return;

             // Check children nodes

             foreach (TreeNode tnChild in tn.Nodes)
             {

                 tnChild.Checked = Checked;

                 SetNodeCheckStatus(tnChild, Checked);

             }

             // Set parent check status

             TreeNode tnParent = tn;

             int nNodeCount = 0;

             while (tnParent.Parent != null)
             {

                 tnParent = (TreeNode)(tnParent.Parent);

                 nNodeCount = 0;

                 foreach (TreeNode tnTemp in tnParent.Nodes)

                     if (tnTemp.Checked == Checked)

                         nNodeCount++;

                 if (nNodeCount == tnParent.Nodes.Count)

                     tnParent.Checked = Checked;

                 else

                     tnParent.Checked = false;

             }

         }

         #region  選擇的節點
         /// <summary>
         /// http://www.codeproject.com/Questions/196856/C-NET-Treeview-Node-Checked
         /// http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.aftercheck.aspx
         /// </summary>
         /// <param name="treeNode"></param>
         /// <param name="nodeChecked"></param>
         public void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
         {
             foreach (TreeNode node in treeNode.Nodes)
             {
                 node.Checked = nodeChecked;
                 if (node.Nodes.Count > 0)
                 {
                     this.CheckAllChildNodes(node, nodeChecked);
                 }
             }
         }
         #endregion
         /// <summary>
         /// 顯示選擇的節點
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private void button1_Click(object sender, EventArgs e)
         {

             DataTable dt = new DataTable();
             dt.Columns.Add("Uid", typeof(Guid));
             dt.Columns.Add("ID", typeof(int));
             dt.Columns.Add("Name", typeof(string));
             dt.Columns.Add("ParentID", typeof(int));

             List<TreeNode> selectedNodes = new List<TreeNode>();
             GetSelectedTreeNode(treeView1.Nodes, selectedNodes);
             int i = 0;
             foreach (TreeNode node in selectedNodes)
             {
                  if (!string.IsNullOrEmpty(node.ToolTipText))
                 {
                     dt.Rows.Add(new Guid(node.ToolTipText), node.Name, node.Text,node.Parent.Name);
                 }
                 i++;
             }

             dataGridView1.DataSource = dt;

         }
         /// <summary>
         /// 選擇的節點的值
         /// </summary>
         /// <param name="nodes"></param>
         /// <param name="selectedNodes"></param>
         void GetSelectedTreeNode(TreeNodeCollection nodes, List<TreeNode> selectedNodes)
         {
             foreach (TreeNode node in nodes)
             {
                 if (node.Checked)
                 {
                     selectedNodes.Add(node);
                 }
                 GetSelectedTreeNode(node.Nodes, selectedNodes);
             }
         }
时间: 2024-09-15 14:10:01

Csharp: Treeview check list value的相关文章

Csharp: TreeView Control

    // --------------------------------------------------------------------------------------------- #region // Copyright (c) 2004-2005, SIL International. All Rights Reserved. // <copyright from='2004' to='2005' company='SIL International'> // Copy

Csharp: TreeView 初始化设置默认选择节点

/// <summary> /// 设置查找的节点为选定节点 /// 涂聚文 /// 2013-07-15 /// </summary> /// <param name="trvToSrch"></param> /// <param name="strToSrch"></param> /// <returns></returns> public TreeNode srch

C#中的TreeView的Check选中问题

问题描述 TreeView如何实现这样的功能1.选中父节点子节点全部选中2.选中部分子节点父节点是方框选中半选中3.选中全部子节点父节点选中就像Windows一样 解决方案 解决方案二:functionOnTreeNodeChecked(){varele=event.srcElement;if(ele.type=='checkbox'){varchildrenDivID=ele.id.replace('CheckBox','Nodes');vardiv=document.getElementBy

ASP.NET 2.0 Treeview Checkboxes - Check All - Javascript

asp.net|javascript|treeview ASP.NET 2.0 TreeView has many built-in features such as showing a checkbox for all the Tree Nodes. Node level formating, style, etc., Enabling the ShowCheckBoxes="All" property sets it to show a checkbox for all the n

VB.net TREEVIEW是通过check来勾选 怎么来获取第N层所有勾选的节点

问题描述 VB.netTREEVIEW是通过checkbox来勾选勾选到的节点显示到datagridview 解决方案 解决方案二:可以通过控件的属性来判断

Asp.net中Treeview终极解决方案

asp.net|treeview|解决 最近在做权限模块,挺头痛的东西.不包括权限部分的业务,还包括权限部分的界面实现.由于采用了微软的Treeview控件来分配权限,所以碰到网上热烈讨论的有关TreeView操作的刷新问题,在众位先人和同事的帮助下,终于使Treeview的问题得到了较好的解决.1.前台控制展开,收缩,Check/UnCheck,后台控制SelectedIndexChanged.解决方法:将autopostback设置成false;  在body里添加  <body  > 

.NET中C#实现C/S架构下的TREEVIEW只需要输入表名,父ID,节点ID,节点名就可以得到树型结构

treeview|架构|树型结构 调用时如下:         /// <param name="newTreeView">树型控件名称</param>        /// <param name="TreeViewName">一层的功能名称</param>        /// <param name="TableName">数据库中的表名</param>       

WPF中TreeView.BringIntoView方法的替代方案

WPF中TreeView.BringIntoView()方法并不是那么地好用,不少时候会没有效果,这里有一个替代方案,调用SelectItem()方法可以展开并呈现TreeView上指定的Item: public static class TreeViewHelper { /// <summary> /// Expands all children of a TreeView /// </summary> /// <param name="treeView"

JS控制TreeView的结点选择_javascript技巧

网上有很多控制TreeView的checkbox选中,但是自己尝试的时候,要么报错,要么不可行.以下这种写法是经过验证有效的,仅作参考,思维比较巧妙! Tree: <asp:TreeView ID="treeViewDapartment" runat="server" SelectedNodeStyle-BackColor="GrayText" onclick="javascript:NodeCheck();" Font