Bind GridView From CSV File Records Using ASP.Net C#

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

Up Next
    Ebook Download
    View all
    Learn
    View all