Prakash Vishwakarma
Prakash Vishwakarma

Reputation: 832

How to make a specific Tree Node to appear as a folder in a Tree View

How to make a specific Tree Node to appear as a folder in a Tree View? I am creating a dynamic Tree View using a table in my database and based on one column i.e., NodeType having value (1 or 2), I want it to either appear as folder or as a normal tree node.

Pseudo code will be more beneficial.

desired result

Thanks in advance!

Upvotes: 2

Views: 304

Answers (1)

VahidNaderi
VahidNaderi

Reputation: 2488

You can put your folder image in an ImageList control and set the ImageList property of the TreeView control to this imagelist and when creating nodes set ImageIndex of that node to the desired index.

    TreeNode tn = new TreeNode();
    if (imageShouldBeFolderImage)
        tn.ImageIndex = 0;

    // If you want to show another image for other cases, 
    // If you want no image ignore this else part
    else            
        tn.ImageIndex = 1;

Update:

If selecting a node changes it's image to another undesirable image it's caused by the TreeView's SelectedImageIndex property, I suggest adding an empty image to the imagelist and set the nodes SelectedImageIndex.

TreeNode tn = new TreeNode();
if (imageShouldBeFolderImage)
{
    tn.ImageIndex = 0;
    tn.SelectedImageIndex = 0;
}
else
{            
    tn.ImageIndex = 1;//the index of the empty image
    tn.SelectedImageIndex = 1;
}

Upvotes: 4

Related Questions