Unable to bind data to chart management

I would like to use a polar diagram in my test application. I have a data table with several columns, where a column named "X" should contain elements of the value x, the rest should be members of the value y. I found the tutorial on MSDN, but it really does not work, because the line

chart1.DataBindTable(dt, "X"); 

will not compile. Any feedback is appreciated and thank you.

Here is the code:

 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; namespace PolarChartTest_01 { public partial class Form1 : Form { public DataTable dt; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { dt.Rows.Clear(); dt.Columns.Clear(); chart1.Series.Clear(); dt.Columns.Add("X", typeof(int)); dt.Columns.Add("Y", typeof(int)); for (int j = 0; j < 7; j++) { DataRow dr = dt.NewRow(); dr["X"] = j * 45; dr["Y"] = j; dt.Rows.Add(dr); } chart1.DataBindTable(dt, "X"); } } 

}

+3
source share
3 answers

It will not compile because the DataTable does not implement the IEnumerable interface. Try:

 var enumerableTable = (dt as System.ComponentModel.IListSource).GetList(); chart1.DataBindTable(enumerableTable , "X"); 
+4
source

It may help you. Write it on the download page.

 chart.DataSource = dataqtableName; chart.Series["seriesName"].XValueMember = "columNameUwantToBind"; chart.Series["seriesName"].YValueMembers = "columNameUwantToBind"; chart.DataBind(); 
+2
source

Another solution might be:

 chart1.DataBindTable(dt.DefaultView, "X"); 

As a bonus, DataView returns this, it can be used for sorting and filtering, except that it is "data bound."

0
source

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


All Articles