This tutorial will show you how to read a csv file in C#.NET

You can design a simple UI allows you to select a file from your disk, then read it and binding data to your DataGridView



To play the demo, you can export the csv file from excel or sql server database. As you can see, your data in the csv file separated by commas.

For example

"CustomerID","CustomerName","Email","Phone"
"01","Tom","tom@gmail.com","222-443-1124"

You can create a class corresponding the columns in csv file, then mapping data from csv to your class

For Example


public class Customer
{
    public string CustomerID { get; set; }
    public string CompanyName { get; set; }
    public string ContactName { get; set; }
    public string ContactTitle { get; set; }
    public string Address { get; set; }
    //...etc
}

Add code to handle your button click event as shown below

string path = "C://data.csv";
string[] lines = System.IO.File.ReadAllLines(path);
foreach(string line in lines)
{
    string[] columns = line.Split(',');
    foreach (string column in columns) {
        //Mapping data to entity
    }
}