Completed
Last Updated: 29 Jun 2016 06:49 by ADMIN
ADMIN
Hristo
Created on: 14 Oct 2015 11:15
Category: TreeView
Type: Bug Report
0
FIX. RadTreeView - NullReferenceException when data source is changed in ValueChanged event
Workaround: create a custom TreeNodeContentElement and override the Synchronize method

public class MyTreeNodeContentElement : TreeNodeContentElement
{
    protected override Type ThemeEffectiveType
    {
        get
        {
            return typeof(TreeNodeContentElement);
        }
    }

    public override void Synchronize()
    {
        TreeNodeElement treeNodeElement = NodeElement;

        if (treeNodeElement == null || treeNodeElement.Data == null || treeNodeElement.Data.TreeViewElement == null || treeNodeElement.Data.Text == null)
        {
            return;
        }

        this.Text = treeNodeElement.Data.Text;
    }

}

public class MyTreeNodeElement : TreeNodeElement
{
    protected override TreeNodeContentElement CreateContentElement()
    {
        return new MyTreeNodeContentElement();
    }

    protected override Type ThemeEffectiveType
    {
        get
        {
            return typeof(TreeNodeElement);
        }
    }
}

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        this.radTreeView1.AllowEdit = true;

        this.radTreeView1.DisplayMember = "name";
        this.radTreeView1.ParentMember = "pid";
        this.radTreeView1.ChildMember = "id";
        this.radTreeView1.DataSource = this.GetSampleData();

        this.radTreeView1.CreateNodeElement += radTreeView1_CreateNodeElement;
        this.radTreeView1.ValueChanged += radTreeView1_ValueChanged;
    }

    private void radTreeView1_CreateNodeElement(object sender, Telerik.WinControls.UI.CreateTreeNodeElementEventArgs e)
    {
        e.NodeElement = new MyTreeNodeElement();
    }

    private void radTreeView1_ValueChanged(object sender, Telerik.WinControls.UI.TreeNodeValueChangedEventArgs e)
    {
       this.UpdateDb();
    }

    private void UpdateDb()
    {
        this.radTreeView1.DataSource = null;
        DataTable dt = this.GetSampleData();
        this.radTreeView1.DataSource = dt;
    }

    private DataTable GetSampleData()
        {
            DataTable dt = new DataTable();

            DataColumn dc = new DataColumn();
            dc.ColumnName = "id";
            dc.DataType = typeof(int);
            dt.Columns.Add(dc);

            DataColumn dc1 = new DataColumn();
            dc1.ColumnName = "name";
            dc1.DataType = typeof(string);
            dt.Columns.Add(dc1);

            DataColumn dc2 = new DataColumn();
            dc2.ColumnName = "pid";
            dc2.DataType = typeof(int);
            dt.Columns.Add(dc2);

            DataRow dr = dt.NewRow();
            dr[0] = 0;
            dr[1] = "My Computer";
            dr[2] = DBNull.Value;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = 1;
            dr[1] = @"C:\";
            dr[2] = 0;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = 2;
            dr[1] = @"D:\";
            dr[2] = 0;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = 3;
            dr[1] = "Program Files";
            dr[2] = 1;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = 4;
            dr[1] = "Microsoft";
            dr[2] = 3;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = 5;
            dr[1] = "Telerik";
            dr[2] = 3;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = 6;
            dr[1] = "WINDOWS";
            dr[2] = 1;
            dt.Rows.Add(dr);

            return dt;
        }
}

(Total attached files size should be smaller than 20mb. Allowed extensions: .zip, .rar, .jpg, .png, .gif)
0 comments