none
DataBinding from Csv file;

    Question

  • using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Windows.Forms;
    using System.Windows.Forms.DataVisualization.Charting;
    using System.IO;
    using System.Data.OleDb;
    
    namespace CandleChartProject
    {
        public partial class Candlestick : System.Windows.Forms.UserControl
        {
            private System.Windows.Forms.DataVisualization.Charting.Chart chart1 = new Chart();
            private System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new ChartArea();
            private System.Windows.Forms.DataVisualization.Charting.Series series1 = new Series();
    
            public Candlestick()
            {
                InitializeComponent();
                InitializeControlSettings();
                this.Controls.Add(chart1);
            }
            private void InitializeControlSettings()
            {
                chart1.BackGradientStyle = GradientStyle.TopBottom;
                chart1.BackColor = Color.LightSteelBlue;
                chart1.BackSecondaryColor = Color.White;
                chart1.BorderlineColor = Color.FromArgb(((int)(((byte)(16)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
                chart1.BorderlineDashStyle = ChartDashStyle.Solid;
                chart1.BorderlineWidth = 1;
                chart1.BorderSkin.SkinStyle = BorderSkinStyle.None;
                chart1.Dock = DockStyle.Fill;
                chartArea1.Area3DStyle.Inclination = 15;
                chartArea1.Area3DStyle.IsClustered = true;
                chartArea1.Area3DStyle.IsRightAngleAxes = false;
                chartArea1.Area3DStyle.Perspective = 10;
                chartArea1.Area3DStyle.PointGapDepth = 0;
                chartArea1.Area3DStyle.Rotation = 10;
                chartArea1.Area3DStyle.WallWidth = 0;
                chartArea1.AxisX.LabelAutoFitStyle = ((System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles)((((System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles.DecreaseFont | System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles.StaggeredLabels)
                            | System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles.LabelsAngleStep30)
                            | System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles.WordWrap)));
                chartArea1.AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.15F, System.Drawing.FontStyle.Bold);
                chartArea1.AxisX.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
                chartArea1.AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
                chartArea1.AxisY.LabelAutoFitStyle = ((System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles)((((System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles.DecreaseFont | System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles.StaggeredLabels)
                            | System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles.LabelsAngleStep30)
                            | System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles.WordWrap)));
                chartArea1.AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.15F, System.Drawing.FontStyle.Bold);
                chartArea1.AxisY.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
                chartArea1.AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
                chartArea1.BackGradientStyle = GradientStyle.TopBottom;
                chartArea1.BackColor = System.Drawing.Color.White;
                chartArea1.BackSecondaryColor = System.Drawing.Color.LightSteelBlue;
                chartArea1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
                chartArea1.BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
                chartArea1.Name = "Default";
                chartArea1.ShadowColor = System.Drawing.Color.Transparent;
                this.chart1.ChartAreas.Add(chartArea1);
    
                this.chart1.Name = "Chart1";
                series1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(16)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
                series1.ChartArea = "Default";
                series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Candlestick;
                series1.Color = System.Drawing.Color.FromArgb(((int)(((byte)(16)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
                series1.Legend = "Default";
                series1.Name = "Series1";
                series1.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Date;
                this.chart1.Series.Add(series1);
                this.chart1.Size = new System.Drawing.Size(700, 596);
                this.chart1.Location = new Point(0, 0);
                this.chart1.TabIndex = 0;
            }
            private void ArrayBinding_Load(object sender, System.EventArgs e)
            {
                BindData();
            }
            private void BindData()
            {
                string file = @"C:\..\..\My Documents\Visual Studio 2008\Projects\CandleChartProject\CandleChartProject\Data\SNP.csv";
                string path = @"C:\Data\";
    
                if (System.IO.File.Exists(path + file))
                {
                    string ConnString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
                    path + ";Extended Properties=\"Text;HDR=Yes;FMT=Delimited\"";
                    OleDbConnection myConnection = new OleDbConnection(ConnString);
                    string mySelectQuery = "Select * from " + file;
                    OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection);
                    // Open the connection and create the reader
                    myCommand.Connection.Open();
                    OleDbDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
                    while (myReader.Read())
                    {
    
                        series1.Points.DataBindXY(myReader, "Date", myReader, "Open,High,Low,Close");
                        series1.ChartType = SeriesChartType.Candlestick;
                        // Close connection and data reader
                        myReader.Close();
                        myConnection.Close();
    
                    }
    
                }
    
            }
        }
    }
    
    Monday, April 06, 2009 10:27 AM

Answers