This is a migrated thread and some comments may be shown as answers.

Checkbox Databinding

1 Answer 160 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Ji -Won
Top achievements
Rank 1
Ji -Won asked on 22 Mar 2012, 10:29 AM
Hello..
Im using radtreeview with my own class.
that class is like this
class TestClass
{
public IsChecked{get;set;}
public Name{get;set;}
}

I used 
radTreeView1.CheckBoxes=true;
radTreeView1.DataSource=bindingList;
radTreeView1.DisplayMember = 
"Name";


I Want if IsChecked Property changed that node's checkstate change.
even I change Checkboxstate make IsChecked change.
but I couldn find bind to CheckboxState..
How Can I bind this data..
please help me and give me solution.. Thanks.

1 Answer, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 27 Mar 2012, 11:49 AM
Hello Ji -Won,

Thank you for writing.

The easiest way to bind the checked field of your class to the nodes check boxes, is to use the NodeFormatting event to set the node check box state and the MouseDown event, of RadTreeView to change the IsChecked field state. With this approach, you need to have in mind that in order to reflect changes to the IsChecked property of an object, you need to call the Update method of the TreeViewElement, which will execute the NodeFormatting event, thus the check boxes will be reassigned. 

Here is a small sample:
public partial class Form1 : Form
 {
     BindingList<TestClass> bindingList = new BindingList<TestClass>();
 
     public Form1()
     {
         InitializeComponent();
 
         bindingList.Add(new TestClass() { IsChecked = false, Name = "Node 1" });
         bindingList.Add(new TestClass() { IsChecked = true, Name = "Node 2" });
         bindingList.Add(new TestClass() { IsChecked = false, Name = "Node 3" });
 
         radTreeView1.CheckBoxes = true;
         radTreeView1.DataSource = bindingList;
         radTreeView1.DisplayMember = "Name";
 
         radTreeView1.NodeFormatting += new Telerik.WinControls.UI.TreeNodeFormattingEventHandler(radTreeView1_NodeFormatting);
         radTreeView1.MouseDown += new MouseEventHandler(radTreeView1_MouseDown);
     }
 
     void radTreeView1_MouseDown(object sender, MouseEventArgs e)
     {
         //when node check box is checked, update the TestClass object
         TreeNodeCheckBoxElement checkBox = radTreeView1.ElementTree.GetElementAtPoint(e.Location) as TreeNodeCheckBoxElement;
         if (checkBox != null)
         {
             TestClass myObject = checkBox.NodeElement.Data.DataBoundItem as TestClass;
             myObject.IsChecked = !checkBox.Checked;
         }
     }
 
     void radTreeView1_NodeFormatting(object sender, Telerik.WinControls.UI.TreeNodeFormattingEventArgs e)
     {
         TestClass myObject = e.Node.DataBoundItem as TestClass;
         if (myObject != null)
         {
             //test the TestClass object IsChecked property ans assign it to the node
             e.Node.Checked = myObject.IsChecked;
         }
     }
 
     private void radButton1_Click(object sender, EventArgs e)
     {
         //update object
         bindingList[0].IsChecked = !bindingList[0].IsChecked;
         //force the control for fire NodeFormatting
         radTreeView1.TreeViewElement.Update(RadTreeViewElement.UpdateActions.Reset);
     }
 
 }
 
 public class TestClass
 {
     public bool IsChecked { get; set; }
     public string Name { get; set; }
 }

I hope that you find this information helpful. Let me know if I can be of further assistance.

Kind regards,
Stefan
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Tags
Treeview
Asked by
Ji -Won
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Share this question
or