using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.IO;

namespace ReadCsv
{
    public partial class frm1ReadCsv : Form
    {
        public frm1ReadCsv()
        {
            InitializeComponent();
        }

        private void frm1ReadCsv_Load(object sender, EventArgs e)
        {
            cityDataGridView.AllowUserToAddRows = false;
            cityDataGridView.AllowUserToDeleteRows = false;
        }

        // Load the data by parsing the CSV file.
        private void loadButton_Click(object sender, EventArgs e)
        {
            // Load the data.
            string[][] values = CsvTools.LoadCsv(fileTextBox.Text);

            // Display the values and elapsed time.
            DisplayValues(values, cityDataGridView);

            // Save the data into a new file.
            File.WriteAllText("test.csv", values.ToCsv());
            // File.WriteAllText("test.csv", TextTools.ToCsv(values));
        }

        // Display an array of arrays of strings in a DataGridView.
        // The first row of strings contains column headers.
        private void DisplayValues(string[][] values, DataGridView dgv)
        {
            int numRows = values.Length - 1;    // Don't count the header row.
            int numCols = values[0].Length;

            // Make column headers (stored in the first row).
            dgv.Columns.Clear();
            foreach (string header in values[0]) dgv.Columns.Add(header, header);

            // Add the required number of rows.
            dgv.Rows.Add(numRows);

            // Add the data.
            for (int r = 1; r <= numRows; r++)
                for (int c = 0; c < numCols; c++)
                    dgv.Rows[r - 1].Cells[c].Value = values[r][c];

            // Autosize the columns.
            dgv.AutoResizeColumns();
        }


        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.fileTextBox = new System.Windows.Forms.TextBox();
            this.loadButton = new System.Windows.Forms.Button();
            this.cityDataGridView = new System.Windows.Forms.DataGridView();
            ((System.ComponentModel.ISupportInitialize)(this.cityDataGridView)).BeginInit();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 17);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(51, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "CSV FIle:";
            // 
            // fileTextBox
            // 
            this.fileTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.fileTextBox.Location = new System.Drawing.Point(69, 14);
            this.fileTextBox.Name = "fileTextBox";
            this.fileTextBox.Size = new System.Drawing.Size(322, 20);
            this.fileTextBox.TabIndex = 1;
            this.fileTextBox.Text = "CityData.csv";
            // 
            // loadButton
            // 
            this.loadButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.loadButton.Location = new System.Drawing.Point(397, 12);
            this.loadButton.Name = "loadButton";
            this.loadButton.Size = new System.Drawing.Size(75, 23);
            this.loadButton.TabIndex = 2;
            this.loadButton.Text = "Load";
            this.loadButton.UseVisualStyleBackColor = true;
            this.loadButton.Click += new System.EventHandler(this.loadButton_Click);
            // 
            // cityDataGridView
            // 
            this.cityDataGridView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.cityDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.cityDataGridView.Location = new System.Drawing.Point(12, 41);
            this.cityDataGridView.Name = "cityDataGridView";
            this.cityDataGridView.Size = new System.Drawing.Size(460, 208);
            this.cityDataGridView.TabIndex = 3;
            // 
            // frm1ReadCsv
            // 
            this.AcceptButton = this.loadButton;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(484, 261);
            this.Controls.Add(this.cityDataGridView);
            this.Controls.Add(this.loadButton);
            this.Controls.Add(this.fileTextBox);
            this.Controls.Add(this.label1);
            this.Name = "frm1ReadCsv";
            this.Text = "ReadCsv";
            this.Load += new System.EventHandler(this.frm1ReadCsv_Load);
            ((System.ComponentModel.ISupportInitialize)(this.cityDataGridView)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox fileTextBox;
        private System.Windows.Forms.Button loadButton;
        private System.Windows.Forms.DataGridView cityDataGridView;
    }
}