Chart for .NET / User's Guide / Data Manipulation / Data Binding / Data Binding

In This Topic
    Data Binding
    In This Topic

    Chart data series can be bound to data sources like DataTable, DataSet, DataAdapter, DataView and custom data sources that implement the IBindingList interface. The data binding functionality is exposed through the NDataBindingManager class.

    The chart control has an associated object of type NDataBindingManager, which is accessible through the DataBindingManager property. The actual data binding is performed with the help of the AddBinding method of the data binding manager. This method binds a data series to a column in a data source. The following examples demonstrate how this method is used.

     Binding to DataTable
    C#
    Copy Code
    chartControl.DataBindingManager.AddBinding(0, 0, "Values", dataTable, "Elevation");
    
    Visual Basic
    Copy Code
    chartControl.DataBindingManager.AddBinding(0, 0, "Values", dataTable, "Elevation")
    

    The first parameter is the index of a chart in the Charts collection. The second parameter is the index of a series in the chart. The third parameter is the name of the property that exposes the data series that should be bound. The fourth parameter is the data source object - in this case a DataTable. The fifth parameter is the "Data Member" - in this case it specifies a column in the DataTable. The meaning of this code is that the Values data series of series 0 in chart 0 will be bound to the "Elevation" column of the dataTable object. 

     Binding to DataSet

    Binding to a DataSet is similar. The only difference is that the last parameter must be in the "TableName.ColumnName" format in order to fully qualify the data source column.

    C#
    Copy Code
    chartControl.DataBindingManager.AddBinding(0, 0, "Values", dataSet, "Table1.Elevation");
    
    Visual Basic
    Copy Code
    chartControl.DataBindingManager.AddBinding(0, 0, "Values", dataSet, "Table1.Elevation")
    
     Binding to DataAdapter

    Binding to a DataAdapter is the same as the previous cases. The fourth parameter is the data adapter object and the fifth parameter is the name of the desired column.

    C#
    Copy Code
    chartControl.DataBindingManager.AddBinding(0, 0, "Values", dataAdapter, "Value");
    
    Visual Basic
    Copy Code
    chartControl.DataBindingManager.AddBinding(0, 0, "Values", dataAdapter, "Value")
    
     Binding to IBindingList

    The component supports data binding to custom data sources that implement the IBindingList interface. If the data source contains simple types like System.Double, you can pass an empty string to the last parameter of the AddBinding method. In case the data source contains objects of some custom type, the last parameter of AddBinding method should be the name of a property of that type. For example if myBindingList contains objects of type MyCustomType, and MyCustomType has a property called ValueY, you can use the following code:

    C#
    Copy Code
    chartControl.DataBindingManager.AddBinding(0, 0, "Values", myBindingList, "ValueY");
    
    Visual Basic
    Copy Code
    chartControl.DataBindingManager.AddBinding(0, 0, "Values", myBindingList, "ValueY")
    

    It is recommended that the custom data sources implement not only the IBindingList interface, but also the ITypedList interface. This ensures that all the objects in the binding list are of the same type and improves the data binding performance.

     Removing Data Bindings
    If you want to remove the data bindings programmatically, you can use one of the following methods - RemoveBindingsForChart, RemoveBindingsForSeries or RemoveAllBindings.
     Related Examples
    Data Manipulation \ Data Binding \ Binding to DataTable
    Data Manipulation \ Data Binding \ Binding to DataAdapter
    Data Manipulation \ Data Binding \ Binding to IBindingList
    See Also