mardi 9 février 2021

Prevent user from altering Check boxes in TreeView

I have a winforms treeview with nodes added and check state set programmatically based on the database values. I am trying to prevent users from altering the check status and am having trouble. I am not sure what event to fire to keep the check state unaltered.

Below is my code:

private void BuildRolesTree(int ParentID, TreeNode pNode, DataSet SourceDS)
    {
        
        DataView dvwData = new DataView(SourceDS.Tables[0]);
        dvwData.RowFilter = "[parent_id] = " + ParentID;
        if (this.InvokeRequired)
        {
            BuildReportTreeDelegate d = new BuildReportTreeDelegate(BuildRolesTree);
            this.Invoke(d, new object[] { ParentID, pNode, SourceDS });
        }
        else
        {
            foreach (DataRowView Row in dvwData)
            {
                TreeNode zNode;

                if (pNode == null)
                    zNode = tv_Permissions.Nodes.Add(Row["node_id"].ToString(), Row["display_access_description"].ToString().Trim());
                else zNode = pNode.Nodes.Add(Row["node_id"].ToString(), Row["display_access_description"].ToString().Trim());

                if (Convert.ToInt32(Row["is_selected"]) == 1)
                    zNode.Checked = true;
                else if (Convert.ToInt32(Row["is_selected"]) == 0)
                    zNode.Checked = false;
                                    


                BuildRolesTree(Convert.ToInt32(Row["node_id"].ToString()), zNode, SourceDS);
            }
        }
    }

    private void PermissionsNode_AfterCheck(object sender, TreeViewEventArgs e)
    {           
       if(e.Action != TreeViewAction.Unknown)
       {
            if(e.Node.Checked) //leave it checked
               e.Node.Checked = e.Node.Checked????
               //I am looking for something like the below
               //e.Checked.NewValue = e.Checked.CurrentValue;
       }    
     }

}
ANy help is appreciated.




Aucun commentaire:

Enregistrer un commentaire