Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have treeview which is loaded from database dynamically with checkbox. when this treeview loaded i want to disable some nodes of this treeview .i.e when user click on these perticular node there is no action perform with this checkbox ....what can i do? which property use?
following is my code:


private void LoadTreeview()
{
//KpiBAL objkpibal = new KpiBAL();
DataSet ds = new DataSet();
ds = balkeyrole.GetTreeviewData(Globalclass.Activedatabase.ToString());
foreach (DataRow dr in ds.Tables["Department"].Rows)
{
TreeNode tn = new TreeNode(dr["DeptName"].ToString());
tn.Tag = dr["DeptID"];
foreach (DataRow drChild in dr.GetChildRows("Dept_sub"))
{
TreeNode tnChild = tn.Nodes.Add(drChild["SubDeptName"].ToString());
tnChild.Tag = drChild["SubDeptId"];

foreach (DataRow dr1 in ds.Tables["SubDepartment"].Rows)
{
foreach (DataRow drChild1 in dr1.GetChildRows("Sub_KPI"))
{
if (drChild["SubDeptId"].ToString() == drChild1["SubDeptId"].ToString())
{
TreeNode kpi = tnChild.Nodes.Add(drChild1["KPIName"].ToString());
kpi.Tag = drChild1["KPIID"];
DataTable Dt = new DataTable();
Dt = balkeyrole.GetKPIID(Globalclass.Activedatabase);
foreach (DataRow dr2 in Dt.Rows )
{
if (dr2["KPIID"].ToString() == kpi.Tag.ToString())
{
//treeViewRoleAssignmentKPI.SelectedNode = kpi;
//ReadOnlyNodeTrue();

treeViewRoleAssignmentKPI.SelectedNode = kpi;
kpi.Checked = true; //this code i will try for disable but cant work
kpi.EndEdit(false);
// kpi.EnsureVisible();
// kpi.EndEdit(false);
//ReadOnlyNodeTrue(kpi);
// treeViewRoleAssignmentKPI.CheckBoxes=true;
//HideCheckBox(ref kpi);
}
}
}
}
}
}
treeViewRoleAssignmentKPI.Nodes.Add(tn);
treeViewRoleAssignmentKPI.SelectedNode = treeViewRoleAssignmentKPI.TopNode;
colorTreeViewNodes(treeViewRoleAssignmentKPI.Nodes);
}
}
Posted
Comments
Dinesh.V.Kumar 17-Feb-14 5:12am    
try ..kpi.Enabled=false...

Regards,

Maybe this helps:
http://forums.asp.net/t/1245057.aspx?How+to+create+a+flipcart+like+panel+for+showing+products+in+gridview
 
Share this answer
 
... edit #1 ...

With a quick search I found this Win API code for disabling CheckBoxes in a TreeView: [^].

This would require you to sub-class the Microsoft TreeView, something I tried in the past and was not happy with the results of for various reasons.

... end edit #1 ...

Assuming this is a Windows Forms TreeView: there is some good news; you can prevent CheckBox activation from "casual clicking" by doing the following:
C#
// a List to hold TreeNodes with CheckBox disabled
List<TreeNode> DisabledCheckNodes = new List<TreeNode>();

// method to disable CheckBox use in specific TreeNodes
private void SetDisabledCheckNodes(params TreeNode[] theNodes)
{
    DisabledCheckNodes.AddRange(theNodes);
}

// method to re-enable CheckBox use in specific TreeNodes
private void EnableCheckNodes(params TreeNode[] theNodes)
{
    foreach (var theNode in theNodes)
    {
        if (DisabledCheckNodes.Contains(theNode)) DisabledCheckNodes.Remove(theNode);
    }
}

// a flag to control possible recursion in changing
// TreeNode CheckState in a 'CheckChanged EventHandler
private bool dontRecurse = false;

// function to use to try and prevent CheckBox use
private bool StopCheckBoxUse(TreeNode theNode)
{
    if (DisabledCheckNodes.Contains(theNode))
    {
        dontRecurse = true;
        theNode.Checked = false;
        treeView1.Update();
        dontRecurse = false;
        return true;
    }
    else
    {
        return false;
    }
}

// the EventHandlers for the TreeView
private void treeView1_BeforeCheck(object sender, TreeViewCancelEventArgs e)
{
    if (dontRecurse) return;

    e.Cancel = StopCheckBoxUse(e.Node);
}

private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
    if (dontRecurse) return;

    StopCheckBoxUse(e.Node);
}
To set which Nodes have disabled CheckBoxes:
C#
SetDisabledCheckNodes(treeView1.Nodes[2],treeView1.Nodes[4],treeView1.Nodes[6]);
To re-enable CheckBoxes on a TreeNode:
EnableCheckNodes(treeView1.Nodes[6]);
And now, the bad news; the user, if determined, can get the CheckBox Checked, or unchecked by quickly repeated clicking on the CheckBox.

I experimented (long ago) with defining every TreeView Event that could possibly affect the CheckBox state, all MouseClicks, MouseDowns, After-Before Select, etc., and set the CheckBox to unchecked in them, without success. I have concluded that this phenomenon is yet another quirk of the Win Form TreeView (another reason, among many, I switched to using the commercial Lidor Systems IntegralUI TreeView).

However, this code was done several years ago, and I have not researched, recently, to see if there is any other "fix" available for making disabling the CheckBoxes "stick" under all conditions for the WinForm TreeView.
 
Share this answer
 
v2
Comments
Gauri Bharde 18-Feb-14 1:01am    
Thanks sir i have tried this code but same problem occurs as mentioned above,is any solution to removing checkbox for specific node? It means using above code disable perticuler node,if is possible to remove checkbox for these perticuler node?
BillWoodruff 18-Feb-14 2:21am    
Please see the edit to my first response for additional information.

I do not believe you can remove CheckBoxes from specific Nodes in a Microsoft WinForm TreeView. As I said, this behavior with CheckBoxes was one factor in my deciding to no longer use the standard TreeView and purchase the Lidor IntegralUI TreeView which is an amazing Control.

Do check out several alternative TreeView Controls presented in articles here on CP:

http://www.codeproject.com/search.aspx?q=treelistview&x=0&y=0&sbo=kw

To see if any of them offer the feature you need.

Check out Philip Piper's TreeListView here on CodeProject for an alternative; this great Control has been in development here for over six years:

http://www.codeproject.com/Articles/16009/A-Much-Easier-to-Use-ListView

I don't have time to go and check out these CP resources to see what they offer in terms of CheckBox disabling.

Gauri Bharde 18-Feb-14 3:46am    
thanks sir.
BillWoodruff 18-Feb-14 4:18am    
Namaste, No need to call me "Sir" :) Have you done any WinAPI programming using an over-ride of WndProc ? This is a Windows Forms TreeView we are discussing here ... right ?
Gauri Bharde 20-Feb-14 0:05am    
yes ....this is Win form application we are discussing and i have not done any WinAPI programming using an over-ride of WndProc.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900