Home > Code Samples > Display CSV file in GridView in C#
Display a CSV File in a GridView on an ASP.Net page
C#
protected void Page_Load(object sender, EventArgs e)
{
// declare a new GridView
GridView csvGrid = new GridView();
// add GridView to page
Form.Controls.Add(csvGrid);
// declare csv parser passing in path to file
using (CsvReader csvData = new CsvReader(Server.MapPath("products.csv")))
{
// set GridView to use DataTable returned from parser as the source
// for it's data. True is passed in to signify that file contains a
// header row which will name returned DataColumn's based on values
// read in from header row.
csvGrid.DataSource = csvData.ReadToEnd(true);
} // dispose of parser
// tell GridView to create display based on values from DataSource
csvGrid.DataBind();
}
|