Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

Binding a chart to a datagridview

$
0
0
I am trying to create a DataGridView that is bound to a Chart object so that the values in the DataGridView are represented in the Chart.

In essence, I want to bind the data between both objects. So far I've come up with this:
           
BindingSource bs = new BindingSource();
DataTable table = new DataTable();
table.Columns.Add(new DataColumn("X", typeof(float)));
table.Columns.Add(new DataColumn("Y", typeof(float)));
bs.DataSource = table;
DataGridView data = new DataGridView(); 
data.DataSource = bs;  
data.CellValueChanged += ValueChanged;  
NChart chart = new NChart();     
Series series = new Series("Series");   
chart.Series.Add(series);
series.Points.DataBindXY(bs) 
//----  
static void ValueChanged(object sender, EventArgs e) 
{
   DataGridView o = sender as DataGridView;
   BindingSource b = o.DataSource as BindingSource;
}

This compiles just fine, however the program results in an immediate run-time error, stating that the Y-values (which would be the second argument in series.Points.DataBindXY()) are empty. I've tried using table.Columns[0] and `table.Columns[1] but they don't inherit from IEnumerable<>, so that doesn't compile. 

As well, the program is meant to start with an empty DataGridView, so empty Y-values should be considered. Or does the error simply imply that no object containing would-be Y-values is given, even if such an object is empty?

Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>