New to Telerik UI for WinFormsStart a free 30-day trial

Binding check boxes

Updated over 6 months ago

Since R3 2014 RadTreeView supports binding the check-boxes of the nodes to a field in the data. You just need to specify the RadTreeView. CheckedMember property. The following code snippet demonstrates how to setup hierarchical tree by using two separate DataTables:

WinForms RadTreeView Binding check boxes

C#
            
DataTable parentDt = new DataTable("Parent");
parentDt.Columns.Add("MasterId", typeof(string));
parentDt.Columns.Add("Title", typeof(string));
parentDt.Columns.Add("IsActive", typeof(bool));
            
DataTable childDt = new DataTable("Child");
childDt.Columns.Add("ChildId", typeof(string));
childDt.Columns.Add("ParentId", typeof(string));
childDt.Columns.Add("Name", typeof(string));
childDt.Columns.Add("Status", typeof(bool));
            
string parentId = string.Empty;
string childId = string.Empty;
for (int i = 1; i <= 5; i++)
{
    parentId = Guid.NewGuid().ToString();
    parentDt.Rows.Add(parentId, "Node." + i, i % 2 == 0);
    
    for (int j = 1; j < 5; j++)
    {
        childId = Guid.NewGuid().ToString();
        childDt.Rows.Add(childId, parentId, "SubNode." + i + "." + j, j % 2 == 0);
    }
}
            
radTreeView1.DataSource = parentDt;
radTreeView1.RelationBindings.Add(new RelationBinding(childDt,null,"Name","MasterId","ParentId","ChildId","Status"));
radTreeView1.DisplayMember = "Title";
radTreeView1.ValueMember = "Id";
radTreeView1.CheckedMember = "IsActive";
radTreeView1.CheckBoxes = true;

Binding CheckBoxes with Object Relational Hierarchy

Consider the following diagram which can be illustrated with the sample classes below: WinForms RadTreeView Sample Classes

Note that the IsActive and the Status properties represent boolean data.

C#
    
public class Parent 
{
    public string ParentId { get; set; }
    
    public string Title { get; set; }
    
    public bool IsActive { get; set; }
    
    public List<Child> Children { get; set; }
        
    public Parent(string parentId, string title, bool isActive, List<Child> children)
    {
        this.ParentId = parentId;
        this.Title = title;
        this.IsActive = isActive;
        this.Children = children;
    }
}
    
public class Child
{
    public string ChildId { get; set; }
    
    public string ParentId { get; set; }
    
    public string Name { get; set; }
    
    public bool Status { get; set; }
        
    public Child(string childId, string parentId, string name, bool status)
    {
        this.ChildId = childId;
        this.ParentId = parentId;
        this.Name = name;
        this.Status = status;
    }
}

The code snippet below illustrates how to bind the check-boxes coming from the described properties:

WinForms RadTreeView Binding CheckBoxes with Object Relational Hierarchy

C#
            
List<Parent> dataItems = new List<Parent>();
Parent currentParent;
Child currentChild;
List<Child> children;
string parentId = string.Empty;
string childId = string.Empty;
for (int i = 1; i <= 5; i++)
{
    parentId = Guid.NewGuid().ToString();
        
    children = new List<Child>();
    for (int j = 1; j < 5; j++)
    {
        childId = Guid.NewGuid().ToString();
        currentChild = new Child(childId, parentId, "SubNode." + i + "." + j, j % 2 == 0);
        children.Add(currentChild);
    }
    currentParent = new Parent(parentId, "Node." + i, i % 2 == 0,children);
    dataItems.Add(currentParent);
}
            
radTreeView1.DataSource = dataItems;
radTreeView1.DisplayMember = "Title\\Name";
radTreeView1.ChildMember = "Parent\\Children";
radTreeView1.CheckedMember = "IsActive\\Status";
radTreeView1.CheckBoxes = true;

See Also