Completed
Last Updated: 20 Feb 2014 15:26 by ADMIN
ADMIN
Dess | Tech Support Engineer, Principal
Created on: 20 Dec 2013 09:30
Category: TreeView
Type: Bug Report
0
FIX. RadTreeView throws ArgumentOutOfRangeException after clearing the DataSource
To reproduce:
-add RadTreeView and RadButton:
-use the following code:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        List<Distributor> distributors = new List<Distributor>()
        {
            new Distributor(432,"Distributor 1"),
            new Distributor(454,"Distributor 2"),
            new Distributor(438,"Distributor 3"),
            new Distributor(455,"Distributor 4")
        };

        List<Distributor> distributors2 = new List<Distributor>()
        {
            new Distributor(486,"Distributor 5"),
            new Distributor(487,"Distributor 6"),
            new Distributor(498,"Distributor 7"),
            new Distributor(475,"Distributor 8")
        };

        List<Product> products = new List<Product>();
        products.Add(new Product(567, "Bicycle", 5, distributors));
        products.Add(new Product(456, "Car", 5000,distributors2));
        products.Add(new Product(789, "Bike", 1500,null));

        BindingList<Category> categories = new BindingList<Category>();
        categories.Add(new Category("Bikes", products));
        categories.Add(new Category("Accessories", null));
        categories.Add(new Category("Clothing", null));

        radTreeView1.DataSource = categories;
        if ((radTreeView1.DataSource as BindingList<Category>).Count > 0)
        {
            radTreeView1.DisplayMember = "Name\\Description\\Name";
            radTreeView1.ChildMember = "Categories\\Products\\Distributors";   
        }
        
    }

    private void radButton1_Click(object sender, EventArgs e)
    {
        BindingList<Category> categories = radTreeView1.DataSource as BindingList<Category>;
        categories.Clear();
    }
}

public class Distributor
{
    private int _id;
    private string _name;

    public Distributor(int id, string name)
        {
            this._id = id;
            this._name = name;
        }

    public int Id
        {
            get
            {
                return this._id;
            }
            set
            {
                this._id = value;
            }
        }

    public string Name
        {
            get
            {
                return this._name;
            }
            set
            {
                this._name = value;
            }
        }
}

public class Product
{
    private int _id;
    private string _description;
    private float _price;
    private List<Distributor> _distributors;

    public List<Distributor> Distributors
        {
            get
            {
                return this._distributors;
            }
            set
            {
                this._distributors = value;
            }
        }

    public int ID
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }

    public string Description
        {
            get
            {
                return _description;
            }
            set
            {
                _description = value;
            }
        }

    public float Price
        {
            get
            {
                return _price;
            }
            set
            {
                _price = value;
            }
        }

    public Product(int id, string description, float price, List<Distributor> distributors)
    {
        _id = id;
        _description = description;
        _price = price;
        _distributors = distributors;
    }
}

public class Category
{
    public Category(string name, List<Product> products)
    {
        _name = name;
        _products = products;
    }

    private List<Product> _products;
    private string _name;

    public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }

    public List<Product> Products
        {
            get
            {
                return _products;
            }
            set
            {
                _products = value;
            }
        }
}

When you click the button, ArgumentOutOfRangeException is thrown.

Workaround: instead of clearing the BindingList, remove each list item one by one:
while (categories.Count>0)
{
    categories.RemoveAt(0);
}
0 comments