从datagridview制作图表

时间:2014-08-17 08:59:28

标签: datagridview charts datatable

我有一个datagridview用户填写它。计算结果并返回datagridview的只读列。 我想为我的datagridview的两列创建一个图表。但是我没有在表格中保存datagridview,因为表格中有先前的结果! 有人告诉我创建图表你应该将datagridview保存到表中,但在这种情况下你应该将datagridview转换为datatable然后清除它! 请告诉我该怎么办! :( 这是我创建图表的代码,但它返回一个空图表!(我的datagridview中有19列)

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();
        }
    }


}

}

1 个答案:

答案 0 :(得分:0)

数据系列的X和Y值可以使用DataBindXY方法绑定到数据源,因此我同意您在这种情况下使用它是最合适的。 我相信这里真正的问题不是从DataGridView控件本身获取数据(只是一些数据的容器,与图表相同),而是使用DataGridView.DataSource作为图表。 无论您的原始数据源是什么:DataTable,IDbDataReader,csv文件,Excel,Access数据库等等,都使用它来进行绑定而不是DataGridView。

// considering you have 
this.DGV = someDataSource;
// use this instead
chart.Series["Series"].Points.DataBindXY(someDataSource, "Time", someDataSource, "Concentration");
//lose the additional DataTable creation