title: Reading and displaying data from CSV file on Windows Form Application using C#
body: <p>I have written the following code for reading data from .csv file:</p>
  
  <pre><code>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 CSVRead
  {
      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
          }
          private void buttonRead_Click(object sender, EventArgs e)
          {
              DataTable dataTable = new DataTable();
              dataTable.Columns.Add("Username");
              dataTable.Columns.Add("Password");
              dataTable.Columns.Add("MachineID");
              string filePath = textBox1.Text;
              StreamReader streamReader = new StreamReader(filePath);
              string[] totalData = new string[File.ReadAllLines(filePath).Length];
              totalData = streamReader.ReadLine().Split(',');
              while (!streamReader.EndOfStream)
              {
                  totalData = streamReader.ReadLine().Split(',');
                  dataTable.Rows.Add(totalData[0], totalData[1], totalData[2]);
              }
              dataGridView1.DataSource = dataTable;
          }
  
          private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
          {
  
          }
      }
  }
  </code></pre>
  
  <p>Here is my CSV File data (readCSV.csv):</p>
  
  <pre><code>Username, Password, MachineID
  abc, abc, 123
  jkl, jkl, 789
  rst, rst, 456
  </code></pre>
  
  <p>I have a <strong>dataGridView</strong> in my <strong>Windows Form Application</strong> (see image link below as I haven't accumulated enough reputation to post an image) and want to display data from CSV file in this grid view. This code isn't throwing any error/warning but it is simply not executing the way it should. On clicking the Find button the data is not getting displayed in the <strong>dataGridView</strong>. I am using Visual Studio 2013 Professional.</p>
  
  <p><a href="https://i.stack.imgur.com/gqB0s.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gqB0s.png" alt="WinFormApp"></a></p>
  
  <p><strong>Silly Me:</strong> Oops!!! The above code is working absolutely fine... I was writing my code on Remote machine and I stored my file on local machine. Also, the name of button click event was mistyped.</p>
  
  <p><strong>NOTE:</strong>  The answer has been marked as accepted because its logic works too. The code written above in my question is also a working absolutely fine</p>