Background
I have often read the common question in forum posts of how to bind a GridView from CSV file records but no one has provided the proper solution and many solutions contain a lot of code that is not required so by considering the preceding requirements I have decided to write this article to provide the solution to bind a GridView from CSV file records with a minimum amount of code. So let us start creating an application so beginners can also understand. First create the CSV file named Employee as:
Now we have records to bind to the GridView, let us create the sample web application as follows:
  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
  2. "File" - "New WebSite" - "C#" - "Empty WebSite" (to avoid adding a master page).
  3. Provide the web site a name such as "BindGridviewFromCSVFileRecords" or another as you wish and specify the location.
  4. Then right-click on Solution Explorer and select "Add New Item" and Add Web Form.
  5. Drag and drop one Button, a GridView and a FileUploader control onto the <form> section of the Default.aspx page.

Now the default.aspx page source code will look such as follows.

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head id="Head1" runat="server">
  5. <title>Article by Vithal Wadje</title>
  6. </head>
  7. <body bgcolor="blue">
  8. <form id="form1" runat="server">
  9. <div style="color: White;">
  10. <h4>
  11. Article for C#Corner
  12. </h4>
  13. <table>
  14. <tr>
  15. <td>
  16. Select File
  17. </td>
  18. <td>
  19. <asp:FileUpload ID="FileUpload1" runat="server" />
  20. </td>
  21. <td>
  22. </td>
  23. <td>
  24. <asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" />
  25. </td>
  26. </tr>
  27. </table>
  28. <br />
  29. <br />
  30. <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
  31. <AlternatingRowStyle BackColor="White" />
  32. <EditRowStyle BackColor="#7C6F57" />
  33. <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
  34. <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
  35. <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
  36. <RowStyle BackColor="#E3EAEB" />
  37. <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
  38. <SortedAscendingCellStyle BackColor="#F8FAFA" />
  39. <SortedAscendingHeaderStyle BackColor="#246B61" />
  40. <SortedDescendingCellStyle BackColor="#D4DFE1" />
  41. <SortedDescendingHeaderStyle BackColor="#15524A" />
  42. </asp:GridView>
  43. </div>
  44. </form>
  45. </body>
  46. </html>
Now write the following code in a button click to upload and read the CSV File records as:
  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3. //Creating object of datatable
  4. DataTable tblcsv = new DataTable();
  5. //creating columns
  6. tblcsv.Columns.Add("Name");
  7. tblcsv.Columns.Add("City");
  8. tblcsv.Columns.Add("Address");
  9. tblcsv.Columns.Add("Designation");
  10. //getting full file path of Uploaded file
  11. string CSVFilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);
  12. //Reading All text
  13. string ReadCSV = File.ReadAllText(CSVFilePath);
  14. //spliting row after new line
  15. foreach (string csvRow in ReadCSV.Split('\n'))
  16. {
  17. if (!string.IsNullOrEmpty(csvRow))
  18. {
  19. //Adding each row into datatable
  20. tblcsv.Rows.Add();
  21. int count = 0;
  22. foreach (string FileRec in csvRow.Split(','))
  23. {
  24. tblcsv.Rows[tblcsv.Rows.Count - 1][count] = FileRec;
  25. count++;
  26. }
  27. }
  28. //Calling Bind Grid Functions
  29. Bindgrid(tblcsv);
  30. }
  31. }
Create the function to bind the GridView as:
  1. //Function to bind gridview
  2. private void Bindgrid(DataTable csvdt)
  3. {
  4. GridView1.DataSource = csvdt;
  5. GridView1.DataBind();
  6. }
The entire code of the default.aspx.cs page will look as follows:
  1. using System;
  2. using System.IO;
  3. using System.Data;
  4. public partial class _Default : System.Web.UI.Page
  5. {
  6. protected void Page_Load(object sender, EventArgs e)
  7. {
  8. }
  9. protected void Button1_Click(object sender, EventArgs e)
  10. {
  11. //Creating object of datatable
  12. DataTable tblcsv = new DataTable();
  13. //creating columns
  14. tblcsv.Columns.Add("Name");
  15. tblcsv.Columns.Add("City");
  16. tblcsv.Columns.Add("Address");
  17. tblcsv.Columns.Add("Designation");
  18. //getting full file path of Uploaded file
  19. string CSVFilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);
  20. //Reading All text
  21. string ReadCSV = File.ReadAllText(CSVFilePath);
  22. //spliting row after new line
  23. foreach (string csvRow in ReadCSV.Split('\n'))
  24. {
  25. if (!string.IsNullOrEmpty(csvRow))
  26. {
  27. //Adding each row into datatable
  28. tblcsv.Rows.Add();
  29. int count = 0;
  30. foreach (string FileRec in csvRow.Split(','))
  31. {
  32. tblcsv.Rows[tblcsv.Rows.Count - 1][count] = FileRec;
  33. count++;
  34. }
  35. }
  36. //Calling Bind Grid Functions
  37. Bindgrid(tblcsv);
  38. }
  39. }
  40. //Function to bind gridview
  41. private void Bindgrid(DataTable csvdt)
  42. {
  43. GridView1.DataSource = csvdt;
  44. GridView1.DataBind();
  45. }
  46. }

Now run the application and the UI will look as follows:
Now select the file by using the browse button as:
Now click on the Upload button. The records in the GridView will then look as in the following:
Now you have seen how the records are displayed in the GridView using a CSV file with a minimal amount of code and effort.
Note
  • Do a proper validation such as date input values when implementing.
Summary

From all the preceding examples you have learned how to bind a GridView using CSV file records. I hope this article is useful for all readers, if you have a suggestion then please contact me.