Windows Forms: Read & Write csv file in C#

How to read and write csv file to DataGridView using CsvHelper in C#

Step 1Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "ReadWriteCsvFile" and then click OK

c# csv fileStep 2: Right click on your project select Manage NuGet Packages -> Search csvhelper -> Install

c# csvhelperA library for reading and writing CSV files. Extremely fast, flexible, and easy to use. Supports reading and writing of custom class objects.

Step 3: Design your form as below

csvhelper in c#

Step 4: Create a Student class to map data as below

public class Student
{
    public string StudentID { get; set; }
    public string StudentName { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
}

Step 5: Add code to button click event handler as below

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

namespace ReadWriteCsvFile
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnWrite_Click(object sender, EventArgs e)
        {
            using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "CSV|*.csv", ValidateNames = true })
            {
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    //Save datagridview to csv file
                    using (var sw = new StreamWriter(sfd.FileName))
                    {
                        var writer = new CsvWriter(sw);
                        writer.WriteHeader(typeof(Student));
                        foreach(Student s in studentBindingSource.DataSource as List<Student>)
                        {
                            writer.WriteRecord(s);
                        }
                    }
                    MessageBox.Show("Your data has been successfully saved.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Load data to binding source
            studentBindingSource.DataSource = new List<Student>();
        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            using(OpenFileDialog ofd= new OpenFileDialog() { Filter = "CSV|*.csv", ValidateNames = true })
            {
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    //Read csv file to datagridview
                    var sr = new StreamReader(new FileStream(ofd.FileName, FileMode.Open));
                    var csv = new CsvReader(sr);
                    studentBindingSource.DataSource = csv.GetRecords<Student>();
                }
            }
        }
    }
}

VIDEO TUTORIALS