how to get checked node from treeview

2 posts, 0 answers
  1. SachinC
    SachinC avatar
    23 posts
    Member since:
    Sep 2010

    Posted 22 Oct 2010 Link to this post

    Hello,

    I am using radtreeview. and added rootnode and child nodes to it,showing checkbox for each node (parent and child node).
    after that i am selecting many nodes (Checkbox). after clicking remove button. i need to remove the checked nodes from treeview as well as from database.

    So, how to get the collection of checked nodes?

    Please help?

    thanks
    Sachin C
  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 22 Oct 2010 Link to this post

    Hi Sachin,

    This will get you a collection of nodes that are checked. I've adapted the code at this Code Library Article

    Private m_CheckedNodes As New System.Collections.Generic.List(Of Telerik.WinControls.UI.RadTreeNode)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        m_CheckedNodes.Clear()
        For Each node As Telerik.WinControls.UI.RadTreeNode In RecursiveTreeTraversal(Me.RadTreeView1.Nodes)
            MessageBox.Show(node.Text)
        Next
    End Sub
    Private Function RecursiveTreeTraversal(ByVal levelNodes As Telerik.WinControls.UI.RadTreeNodeCollection) As System.Collections.Generic.List(Of Telerik.WinControls.UI.RadTreeNode)
        For Each node As Telerik.WinControls.UI.RadTreeNode In levelNodes
            If node.Checked Then
                m_CheckedNodes.Add(node)
            End If
            RecursiveTreeTraversal(node.Nodes)
        Next
        Return m_CheckedNodes
    End Function

    and in C#
    private System.Collections.Generic.List<Telerik.WinControls.UI.RadTreeNode> m_CheckedNodes = new System.Collections.Generic.List<Telerik.WinControls.UI.RadTreeNode>();
    private void Form1_Load(System.Object sender, System.EventArgs e)
    {
        m_CheckedNodes.Clear();
        foreach (Telerik.WinControls.UI.RadTreeNode node in RecursiveTreeTraversal(this.RadTreeView1.Nodes)) {
            MessageBox.Show(node.Text);
        }
    }
      
    private System.Collections.Generic.List<Telerik.WinControls.UI.RadTreeNode> RecursiveTreeTraversal(Telerik.WinControls.UI.RadTreeNodeCollection levelNodes)
    {
        foreach (Telerik.WinControls.UI.RadTreeNode node in levelNodes) {
            if (node.Checked) {
                m_CheckedNodes.Add(node);
            }
            RecursiveTreeTraversal(node.Nodes);
        }
      
        return m_CheckedNodes;
    }

    hope that helps
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top