Click here to Skip to main content
15,878,748 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a datagridview which user fill it. the result is calculated and return to read only columns of datagridview. I want to make a chart for two columns of my datagridview. but I don't save datagridview in a table because there is previous results in the table! somebody told me for creating chart you should save your datagridview to a table but in this case you should convert datagridview to datatable and then clear it! please tell me what should I do! :( this is my code for creating chart but it returns a empty chart!( I have 19 columns in my datagridview)

C#
 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 finalproject
{
    public partial class Time_chart_Form : Form
    {
        private DataGridView DGV;
        public Time_chart_Form(DataGridView DGV)
        {
            InitializeComponent();
            this.DGV = DGV;
        }

        private void Time_chart_Form_Load(object sender, EventArgs e)
        {


            using (DataTable table = new DataTable("MyTable"))
            {

                DataRow newRow;

                table.Columns.Add(new DataColumn("Time", typeof(string)));

                table.Columns.Add(new DataColumn("Concentration", typeof(string)));



                int column;

                for (int i = 0; i < this.DGV.Rows.Count; i++)
                {

                    column = 0;

                    newRow = table.NewRow();

                    if (!this.DGV[column, i].FormattedValue.Equals(""))

                        newRow["Time"] = this.DGV[0, i].Value.ToString();

                    else

                        newRow["Time"] = null;

                    column = 16;

                    if (!this.DGV[16, i].FormattedValue.Equals(""))

                        newRow["Concentration"] = this.DGV[column, i].Value.ToString();

                    else

                        newRow["Concentration"] = null;

                    table.Rows.Add(newRow);

                }





                Chart chart = new Chart();



                chart.Width = 1600;
                chart.Height = 900;

                Series Series = new Series();
                Series.Name = "Series";

                Series.Color = Color.Red;

                Series.BorderColor = Color.FromArgb(255, 0, 0);
                Series.ChartType = SeriesChartType.Point;

                Series.BorderDashStyle = ChartDashStyle.Solid;
                //Series.BorderWidth = 10000;


                Series.ShadowColor = Color.FromArgb(128, 128, 128);
                Series.ShadowOffset = 0;

                Series.BorderColor = Color.FromArgb(0, 0, 0);



                //Chart Area-------------------------

                ChartArea ca1 = new ChartArea();
                ca1.Name = "ChartArea";
                ca1.BackColor = Color.White;
                ca1.BorderColor = Color.FromArgb(255, 255, 255);
                ca1.BorderWidth = 10;
                ca1.BorderDashStyle = ChartDashStyle.Solid;
                ca1.AxisX = new Axis();
                ca1.AxisY = new Axis();

                chart.BackColor = Color.FromArgb(255, 255, 255);
                chart.BackSecondaryColor = Color.White;
                chart.BackGradientStyle = GradientStyle.TopBottom;
                ca1.BackColor = System.Drawing.Color.FromArgb(64, System.Drawing.Color.White);

                chart.ChartAreas.Add(ca1);


                chart.Series.Add(Series);

                chart.Series["Series"].BorderWidth = 500;



                chart.Series["Series"].Points.DataBindXY(table.DefaultView, DGV.Columns[0].Name, table.DefaultView, DGV.Columns[16].Name);


                chart.ChartAreas[0].AxisY.Minimum = -6;
                chart.ChartAreas[0].AxisY.Maximum = 6;
                chart.ChartAreas[0].AxisX.Minimum = 0;
                chart.ChartAreas[0].AxisX.Maximum = 10;
                chart.ChartAreas[0].AxisX.Interval = 1;
                chart.ChartAreas[0].AxisY.Interval = 1;

                chart.Titles.Add(new Title("OverView Plot", Docking.Top, new Font("Verdana", 19.5f, FontStyle.Bold), Color.Black));


                chart.ChartAreas[0].AxisY.Title = "Time";
                chart.ChartAreas[0].AxisX.Title = "Concentration";

                chart.ChartAreas[0].AxisX.TitleFont = new System.Drawing.Font("Verdana", 19, FontStyle.Bold);
                chart.ChartAreas[0].AxisY.TitleFont = new System.Drawing.Font("Verdana", 19, FontStyle.Bold);


                chart.ChartAreas[0].AxisX.LabelStyle.Font = new System.Drawing.Font("Verdana", 19.25f, System.Drawing.FontStyle.Bold);
                chart.ChartAreas[0].AxisY.LabelStyle.Font = new System.Drawing.Font("Verdana", 19.25f, System.Drawing.FontStyle.Bold);





                chart.SaveImage(@"C:\Users\Maryam\Documents\Visual Studio 2010\prjchart" + (0).ToString() + ".png", ChartImageFormat.Png);

                //table.Clear();



            }
        }
        
        
    }

}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900