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

Hoa add root node to selfreferenced database binded RadTree

1 Answer 69 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Anoop
Top achievements
Rank 1
Anoop asked on 29 Dec 2012, 06:10 AM
I want to add root node to self referenced database binded RadTree View.
RadContextMenu contextMenu = new RadContextMenu();
RadMenuItem addRootNodeItem = new RadMenuItem("New Node");
addRootNodeItem.Click += addRootNodeItem_Click;
contextMenu.Items.Add(addRootNodeItem);
radTreeView1.RadContextMenu = contextMenu;

void addRootNodeItem_Click(object sender, EventArgs e)
{
RadTreeNode RootNode = new RadTreeNode("New Node");
radTreeView1.Nodes.Add(RootNode);

I tried above code but which returns error Object reference not set to an instant of of an object.
Any other method to do this?

1 Answer, 1 is accepted

Sort by
0
Accepted
Ivan Todorov
Telerik team
answered on 03 Jan 2013, 09:18 AM
Hi Anoop,

Thank you for contacting us.

You are receiving this exception because you are adding an unbound node which does not have an associated row from the underlying data source. To add new nodes to a data bound RadTreeView, you should add records directly to the data source:
private void radButtonAdd_Click(object sender, EventArgs e)
{
    TreeViewTestDataSet.NodeRow row = this.treeViewTestDataSet.Node.NewNodeRow();
    row.SetParentIDNull();
    row.Name = "New Node";
    this.treeViewTestDataSet.Node.AddNodeRow(row);
}

I hope this will help. Should you have any additional questions, feel free to ask.

Kind regards,
Ivan Todorov
the Telerik team
Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
Tags
Treeview
Asked by
Anoop
Top achievements
Rank 1
Answers by
Ivan Todorov
Telerik team
Share this question
or