0

Hi i need to upload my data to the chart from my dataGridView as i load the form i well upload my data after that, the chart have to be created from my dataGridView this is my full code, i dont have problem with the DataGridStatisticFun() , i have aproblem with this function : DataGridToChart(),i am trying to be build the chart but the chart is empty..

    private void StatisticForm_Load(object sender, EventArgs e)
    {
        //load my data to dataGridView
        DataGridStatisticFun();
        //the problem is in this function 
        DataGridToChart();
    }
    //create the chart
    public void DataGridToChart()
    {
        chart1.Series.Clear();
        ChartArea chartArea1 = new ChartArea();
        chartArea1.AxisX.MajorGrid.LineColor = Color.LightGray;
        chartArea1.AxisY.MajorGrid.LineColor = Color.LightGray;
        chartArea1.AxisX.LabelStyle.Font = new Font("Consolas", 8);
        chartArea1.AxisY.LabelStyle.Font = new Font("Consolas", 8);
        chart1.ChartAreas.Add(chartArea1);
        chart1.Series.Add("Series1");
        chart1.Series[0] = new Series();
        chart1.Series[0].XValueMember = dataGridViewStatistics.Columns[0].HeaderText;
        chart1.Series[0].YValueMembers = dataGridViewStatistics.Columns[1].HeaderText;
        chart1.DataSource = dataGridViewStatistics.DataSource;
        chart1.Series[0].ChartType = SeriesChartType.Line;

    }
    public void DataGridStatisticFun()
    {
        int countProducts = dataB.Countstatistics();
        Product[] products = new Product[countProducts];
        products = dataB.Selectstatistics();
        int j = 1, i = 0;
       
        if (countProducts > 0)
        {
          
            countProducts = products.Count(s => s != null);
            dataGridViewStatistics.RowCount = countProducts;
            foreach (Product aProduct in products)
            {

                dataGridViewStatistics[0, i].Value = j++;
                dataGridViewStatistics[1, i].Value = aProduct.Id;
                dataGridViewStatistics[2, i].Value = aProduct.Name;
                dataGridViewStatistics[3, i].Value = aProduct.Count;
                i++;
            }
        }
    }
  • `chart1.DataSource = ` After you have set up the 1st series for databinding you should bind __its Points__, not the whole Chart : `chart1.Series[0].Points.Datasource = dataGridViewStatistics.DataSource;` – TaW Aug 23 '20 at 12:33
  • Can you decipher more? @TaW –  Aug 23 '20 at 13:24
  • Huh? I gave you the correted version of the line with the problem. What happens when you change the code? – TaW Aug 23 '20 at 13:59
  • as i write this code : chart1.Series[0].Points. –  Aug 23 '20 at 14:04
  • Whoops, you are right! There are [various ways to bind a chart..](https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/dd456766(v=vs.100)?redirectedfrom=MSDN) It is done like `aSeries.Points.DataBindXY(datax, datay);` - More [examples](https://stackoverflow.com/search?q=user%3A3152130+chart+databinding) – TaW Aug 23 '20 at 14:10
  • foreach (DataGridViewRow row in dataGridViewStatistics.Rows) { chart1.Series[0].Points.AddXY(row.Cells[2].Value.ToString(), row.Cells[3].Value.ToString()); } –  Aug 23 '20 at 14:14
  • 1
    Yeh its working ! thanks –  Aug 23 '20 at 14:14

0 Answers0