1

I am relatively new to programming and am having trouble with working with CSV files. I am learning C# and using Visual Studio.

I need to have a program that can read, and then create variables from a CSV file that is continually being updated from an outside source.

An example of the CSV file would look as follows:

SPY, 10:00:00am, 182.5

I need my program to create the 182.5 as a variable called Price.

I wrote this, because it was the only way I could find to get the data from the CSV file displayed. I am not even sure if that is the right approach or not, but displaying the data worked:

    private void Form1_Load(object sender, EventArgs e)
    {
        //creates datagridview
        DataGridView dgv = new DataGridView();
        dgv.Location = new System.Drawing.Point(0, 0);
        dgv.Size = new System.Drawing.Size(500, 500);
        this.Controls.Add(dgv);

        //create data source
        DataSet ds = new DataSet();
        ds.Tables.Add();

        dgv.DataSource = ds.Tables[0].DefaultView;

        //load csv

        String[] all_txt = File.ReadAllText(@"C:\Users\Chris\Documents\text.csv").Split('\n');

        int count_row = 0;
        foreach (string row in all_txt)
        {
            string[] all_txt = row.Split(',');
            if (count_row == 0)
            {
                foreach (string header in data)
                {
                    ds.Tables[0].Columns.Add(header);
                }
            }
            else
            {
                ds.Tables[0].Rows.Add(data);
            }
            count_row++;

I am stuck on how to get what is being displayed as a variable

Mike G
  • 4,232
  • 9
  • 40
  • 66
crams
  • 325
  • 1
  • 6
  • 16

1 Answers1

0

Take a look at this great library for dealing with CSV files:

http://joshclose.github.io/CsvHelper/

I've used it on couple of projects and it completely rids you of dealing with typical CSV problems.

Floremin
  • 3,969
  • 15
  • 20