DataTable Data Binding Chart - A Chart Not Updating

I am trying to bind a Chart to a DataTable . I would like the chart to display new rows as they are added, but the chart does not update when new rows are added to the table. I checked that both table and tableDataSource contain new rows, but chart.Series["TestTable"].Points.Count never changes from 5 .

Sample code based on the question Cannot associate datatable with Chart Control , shown below. I would like to either find out if there is an error or omission with the code below, or another, better approach that accomplishes the same goal. I know how to manually add points to a Series , but I would like to see how to do this using data binding.

 Random r = new Random(); Timer timer = new Timer(); DataTable table = new DataTable("TestTable"); DateTime date = new DateTime(2013, 1, 1); IList tableDataSource = null; void timer_Tick(object sender, EventArgs e) { table.Rows.Add(date, r.NextDouble()); date = date.AddDays(1); chart.Update(); } void MainForm_Load(object sender, EventArgs e) { table.Columns.Add("Date", typeof(DateTime)); table.Columns.Add("Percent", typeof(double)); for (int i = 0; i < 5; i++) { table.Rows.Add(date, r.NextDouble()); date = date.AddDays(1); } tableDataSource = (table as IListSource).GetList(); chart.DataBindTable(tableDataSource, "Date"); timer.Interval = 500; timer.Tick += new EventHandler(timer_Tick); timer.Start(); } 
+4
source share
1 answer

Try using the table as a data source instead:

 // tableDataSource = (table as IListSource).GetList(); // chart.DataBindTable(tableDataSource, "Date"); chart.Series.Add("test"); chart.Series["test"].XValueMember = "Date"; chart.Series["test"].YValueMembers = "Percent"; chart.DataSource = table; chart.DataBind(); 

and then in the tick event, call DataBind again instead of Update:

 void timer_Tick(object sender, EventArgs e) { table.Rows.Add(date, r.NextDouble()); date = date.AddDays(1); //chart.Update(); chart.DataBind(); } 
+9
source

Source: https://habr.com/ru/post/1501673/


All Articles