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

Binding to BindingList

Updated over 6 months ago

BindingList is a generic list type, that provides additional control over list items, i.e. the items in RadChartView can be easily edited, removed or added. BindingList also surfaces events that notify when the list has been changed. The example below creates a list of MyCustomObject, initializes the list and assigns it to the BarSeries object in RadChartView.

C#
BindingList<MyCustomObject> myList;
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    myList = new BindingList<MyCustomObject>();
    myList = new BindingList<MyCustomObject>();
    myList.Add(new MyCustomObject(1, "Outdoor"));
    myList.Add(new MyCustomObject(8, "Hardware"));
    myList.Add(new MyCustomObject(3, "Tools"));
    myList.Add(new MyCustomObject(6, "Books"));
    myList.Add(new MyCustomObject(2, "Appliances"));
    BarSeries barSeria = new BarSeries();
    radChartView1.Series.Add(barSeria);
    barSeria.DataSource = myList;
    barSeria.ValueMember = "MyInt";
    barSeria.CategoryMember = "MyString";
}

Figure 1: Binding to BindingList

WinForms RadChartView Binding to BindingList

In order to allow RadChartView to automatically reflect changes in the data source, your object should implement the INotifyPropertyChanged interface:

Binding to BindingList

C#
public class MyCustomObject : INotifyPropertyChanged
{
    private int _myInt;
    private string _myString;
    public MyCustomObject(int myInt, string myString)
    {
        _myInt = myInt;
        _myString = myString;
    }
    public int MyInt
    {
        get { return _myInt; }
        set
        {
            _myInt = value;
            OnPropertyChanged("MyInt");
        }
    }
    public string MyString
    {
        get { return _myString; }
        set
        {
            _myString = value;
            OnPropertyChanged("MyString");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Once the interface is implemented and your collection implement IBindingList, just like the BindingList does, changes are automatically reflected. Here is a sample of adding a new record:

Add Item

C#
private void radButton1_Click(object sender, EventArgs e)
{
    myList.Add(new MyCustomObject(10, "Plants"));
}

Figure 2: Reflect Object Changes

WinForms RadChartView Reflect Object Changes

See Also

In this article
See Also
Not finding the help you need?
Contact Support