Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't bind datatable to Chart Control

I'd like to use a polar chart in my test application. I've a data table with a couple of columns where the column with the name of "X" should provide the x value members, the others should be the y value members. I found a tutorial on MSDN but it doesn't really work because the line

chart1.DataBindTable(dt, "X");

won't compile. Any tip is welcome 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");
    }
}

}

like image 374
JustGreg Avatar asked Dec 19 '25 00:12

JustGreg


1 Answers

It won't compile because DataTable doesn't implement IEnumerable interface. Try:

var enumerableTable = (dt as System.ComponentModel.IListSource).GetList();
chart1.DataBindTable(enumerableTable , "X");
like image 86
tukaef Avatar answered Dec 20 '25 14:12

tukaef