Declined
Last Updated: 08 Feb 2021 10:08 by ADMIN
Jane
Created on: 09 Dec 2020 06:49
Category: TreeView
Type: Bug Report
0
RadTreeView: missing checkboxes for all levels when setting RadTreeNode.CheckType to CheckType.None in the NodeDataBound event

Code snippet for reproducing the problem:

 

    Private Sub RadForm1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.SongsTableAdapter.Fill(Me.MusicCollectionDataSet.Songs)
        Me.ArtistsTableAdapter.Fill(Me.MusicCollectionDataSet.Artists)
        Me.AlbumsTableAdapter.Fill(Me.MusicCollectionDataSet.Albums)

        AddHandler Me.RadTreeView1.NodeDataBound, AddressOf RadTreeView1_NodeDataBound

        Me.RadTreeView1.DataSource = Me.ArtistsBindingSource
        Me.RadTreeView1.DisplayMember = "ArtistName"
        Me.RadTreeView1.ValueMember = "ArtistID"
        Me.RadTreeView1.RelationBindings.Add(New RelationBinding(Me.AlbumsBindingSource, "AlbumName", "ArtistID", "ArtistID", "AlbumID"))
        Me.RadTreeView1.RelationBindings.Add(New RelationBinding(Me.SongsBindingSource, "SongName", "AlbumID", "AlbumID", "SongID"))
        Me.RadTreeView1.CheckBoxes = True
        Me.RadTreeView1.AutoCheckChildNodes = True
        Me.RadTreeView1.TriStateMode = True
        Me.RadTreeView1.ExpandAll()
    End Sub

    Private Sub RadTreeView1_NodeDataBound(sender As Object, e As RadTreeViewEventArgs)
        If e.Node.Level = 0 Then
            e.Node.CheckType = CheckType.None
        Else
            e.Node.CheckType = CheckType.CheckBox
        End If
    End Sub

Workaround: instead of using the NodeDataBound  event, use the NodeFormatting event to hide the checkboxes for the desired nodes:

    Private Sub RadTreeView1_NodeFormatting(sender As Object, e As TreeNodeFormattingEventArgs)
        If e.Node.Level = 0 Then
            e.NodeElement.ToggleElement.Visibility = Telerik.WinControls.ElementVisibility.Collapsed
        Else
            e.NodeElement.ToggleElement.Visibility = Telerik.WinControls.ElementVisibility.Visible
        End If
    End Sub

1 comment
ADMIN
Dimitar
Posted on: 08 Feb 2021 10:07

Hello,

The fix in "RadTreeView: The NodeDataBound event is not firing when binding to a DataTable" raises the NodeDataBound event for all nodes on all levels, prior to it, the event got raised only for the nodes on the top level. The NodeDataBound event fires very early, right when the items get extracted from the data source. At that moment the Level of the node can not be evaluated and is always 0. 

The following code is not valid in this scenario. It worked with the old version simply because the event did not get raised for the child levels, the code in the else clause did not execute any way.

private void radTreeView1_NodeDataBound(object sender, RadTreeViewEventArgs e)
{
    if (e.Node.Level == 0)
    {
        e.Node.CheckType = CheckType.None;
    }
    else
    {
        e.Node.CheckType = CheckType.CheckBox;
    }
}

The correct approach to set the CheckType property of the nodes depending on their level is to handle the RadTreeView.CreateNodeElement event.

private void RadTreeView1_CreateNodeElement(object sender, CreateTreeNodeElementEventArgs e)
{
    if (e.Node.Level == 0)
    {
        e.Node.CheckType = CheckType.None;
    }
    else
    {
        e.Node.CheckType = CheckType.CheckBox;
    }
}

Regards,
Dimitar
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.