sapito
sapito

Reputation: 1522

Windows Forms chart and databinding

I'm trying to create a databinding for a chart on Windows Forms. This is my code:

ch.Series["Dep"].Points.DataBindXY(data, "Date", data, "Value");
ch.DataBind();

Data is of type DataBinding. The issue is that whenever I modify the DataSource:

data.DataSource = ... (list)

The chart doesn't get updated. Actually this same approach works perfectly with widgets like comboboxes (as soon as I update DataBinding.DataSource the widget is updated).

I even tried calling Update() or Refresh() with no results. What am I missing?

Upvotes: 0

Views: 3597

Answers (1)

Crowcoder
Crowcoder

Reputation: 11514

You don't need to call DataBind() at all. DataBindXY is what you want to do when you first bind it, and after you update the data source. For instance, this works:

    public partial class Form1 : Form
    {
        List<int> exes = new List<int> { 1, 3, 7, 9 };
        List<int> whys = new List<int> { 10, 20, 30, 40 };

        public Form1()
       {
            InitializeComponent();
            chart1.Series[0].Points.DataBindXY(exes, whys);
       }

       private void button1_Click(object sender, EventArgs e)
      {
           exes.Add(13);
           whys.Add(50);
           chart1.Series[0].Points.DataBindXY(exes, whys);            
      }
   ...

Upvotes: 1

Related Questions