Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

How to read a CSV file and display on GridView in ASP.NET

Author: Rachel Nicole
Download Source Code :

The purpose of this article is to explain how we can read a CSV file and how we can display it in ASP.NET.


I have written an article on DevASP.NET about how to export a DataTable to CSV file. You can refer to this article if you want to know about creating a CSV file from DataTable. Following article is to export a CSV file back to DataTable so that we can display it on a GridView control. You can read CSV file with different ways but I will explain simplest way in this article.

  1. Open Visual Studio 2010
  2. File > New > Web Site
  3. Visual Basic or Visual C#  > ASP.NET Empty Web Site
  4. Right click on web site > Add New Item > Web Form
  5. Add a GridView in Web Form

    <asp:GridView ID="GridView1" runat="server">

  6. Open code behind file and include following namespaces

    using System.IO;
    using System.Data;

    Imports System.IO
    Imports System.Data
  7. Write below code in Page Load event

    protected void Page_Load(object sender, EventArgs e)
        if (File.Exists("D:\\Employees.csv"))
            string[] data = File.ReadAllLines("D:\\Employees.csv");
            DataTable dt = new DataTable();
            string[] col = data[0].Split(',');
            foreach (string s in col)
                dt.Columns.Add(s, typeof(string));
            for (int i = 0; i < data.Length; i++)
                string[] row = data[i].Split(',');
            GridView1.DataSource = dt;

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If File.Exists("D:\Employees.csv") Then
            Dim data As String() = File.ReadAllLines("D:\Employees.csv")
            Dim dt As New DataTable()
            Dim col As String() = data(0).Split(",")
            For Each s As String In col
                dt.Columns.Add(s, GetType(String))
            For i As Integer = 0 To data.Length - 1
                Dim row As String() = data(i).Split(",")
            GridView1.DataSource = dt
        End If
    End Sub
    Here is the explanation of the above code
    Call ReadAllLines() method by providing file path as string parameter as assign data to string array.
    Create an object of DataTable.
    Create a string array for column in DataTable and get the values of first line in our CSV file. Use the Split() method to split values and provide delimiter in Split() method. I have used Comma (,) and normally Comma is used as delimiter in CSV files.
    Now add each value as column in our DataTable, also provide data type of the column.
    Write a “For” loop to add each row to DataTable.
    Bind DataTable to GridView.
  8. View in browser 
Add Article Comment:
Name :
Email Address :
Comments :
<< How to compare two images in ASP.NET

Disclaimer - Privacy
© 2002-2017