Public Static Class Treeviewcheck { /// <Summary> /// Series node checked attribute Control /// </Summary> /// <Param name = "E"> </param> Public Static Void Checkcontrol (treevieweventargs e ){ If (E. Action! = Treeviewaction. Unknown ){ If (E. node! = Null &&! Convert. isdbnull (E. node) {checkparentnode (E. node ); If (E. node. nodes. Count> 0) {checkallchildnodes (E. node, E. node. Checked );}}}} # Region Private Method // Change the status of all subnodes Private Static Void Checkallchildnodes (treenode Pn, Bool Ischecked ){ Foreach (Treenode TN In Pn. nodes) {tn. Checked = ischecked; If (TN. nodes. Count> 0) {checkallchildnodes (TN, ischecked );}}} // Change the selected status of the parent node. this parameter is used only when all child nodes are not selected. You can modify the selected status as needed. Private Static Void Checkparentnode (treenode curnode ){ Bool Bchecked = False ; If (Curnode. parent! = Null ){ Foreach (Treenode Node In Curnode. Parent. nodes ){ If (Node. Checked) {bchecked = True ; Break ;}} If (Bchecked) {curnode. Parent. Checked = True ; Checkparentnode (curnode. Parent );} Else {Curnode. Parent. Checked = False ; Checkparentnode (curnode. Parent );}}} # Endregion }
Call Method: In the aftercheck event of Treeview
Private void treeviewappsaftercheck (Object sender, treevieweventargs E)
{
Treeviewcheck. checkcontrol (E)
}