开发者

Remove link from treeview parent node

开发者 https://www.devze.com 2023-01-09 04:38 出处:网络
Is there a way I can keep the text of a Parent node but remove the link? The treeview parent node is used just as a header, no navigation. Any help would be great. Thanks.

Is there a way I can keep the text of a Parent node but remove the link? The treeview parent node is used just as a header, no navigation. Any help would be great. Thanks.

private void BindNodes(s开发者_开发百科tring PtID)  
{  
    if (PtID != "")  
    {  
        int PatientID = Convert.ToInt32(PtID);  
        DBConnection dbObj = new DBConnection();  
        if (Session["Activepatient"] != null)  
        {  
            string[] SubChild = new string[4];  
            SubChild[0] = "Demographics";  
            SubChild[1] = "Medication Reviews";  
            SubChild[2] = "Drug Testing & Monitoring";  
            SubChild[3] = "Other Program";
            TreeNode oTrParent = new TreeNode();  
            //trv_patient.ParentNodeStyle = "None";
            //oTrParent.SelectAction.Style.Add("display", "none");
            TreeNode oTrSubChild1;
            TreeNode oTrSubChild;
            for (int i = 0; i < 4; i++)
            {
                oTrSubChild1 = new TreeNode();
                oTrSubChild1.Text = SubChild[i];
                if (i == 1)
                {
                    PatientInfoCollection patientCollection = new PatientInfoCollection();
                    patientCollection = dbObj.GetMedicationReviews(PatientID);
                    foreach (PatientInfo au in patientCollection)
                    {
                        oTrSubChild = new TreeNode();
                        PatientInfo Patient = new PatientInfo();
                        oTrSubChild.Text = au.DateRcvd.Value.ToString("MM-dd-yyyy")
                        oTrSubChild.Target = au.ReviewId.ToString();
                        oTrSubChild1.ChildNodes.Add(oTrSubChild);
                    }
                    oTrSubChild = new TreeNode();
                    oTrSubChild.Text = "Add Medication Review";
                    oTrSubChild.Target = "New";
                    oTrSubChild1.ChildNodes.Add(oTrSubChild);
                }
                trv_patient.Nodes.Add(oTrSubChild1);
            }
        }
    }
}


If temp is such node whose text you want to be plain text and not link then use code below.

TreeNode temp = new TreeNode("text");
temp.SelectAction = TreeNodeSelectAction.None;
treeView1.Nodes.Add(temp);


This is what your looking for. You have to set the SelectAction of the parent node TreeNode to TreeNodeSelectAction.None. I even show how to do it with the child too.

ASP.NET

<asp:TreeView 
  ID="MyTreeView" 
  ShowCheckBoxes="Parent,Leaf,All" 
  runat="server" 
  ShowLines="true" 
  ShowExpandCollapse="true">
</asp:TreeView>

C#

//Make A Parent Node
var Parent = new TreeNode();
Parent.Text = "Parent 1";

// Make the parent nodes not be hyperlinks but plain text
Parent.SelectAction = TreeNodeSelectAction.None;

//You can do the same with a child node like so
var Child = new TreeNode();
Child.Text = "Child 1";

// Make the child nodes not be hyperlinks but plain text
Child.SelectAction = TreeNodeSelectAction.None;

//Add the child node to the parent node
Parent.ChildNodes.Add(Child);

//Finally add the parent node with children to the treeview
MyTreeView.Nodes.Add(Parent);


I'm not sure if I understand what you want. Assuming that it's WinForms and that you just want to disable the ability to select the root node in a TreeView you could just handle the BeforeSelect event of the TreeView and then have the following code:

if (e.Node.Parent == null)
    e.Cancel = true;


TreeNode root = new TreeNode("Root"); root.SelectAction = TreeNodeSelectAction.None;

0

精彩评论

暂无评论...
验证码 换一张
取 消