«

»

Nov 25

Print this Post

Create Gridview from csv file in C sharp (C#)

How to import data into Gridview from csv file by using C sharp ( C# ).

Soln:

//Read the data from text file

string[] textData = System.IO.File.ReadAllLines(Server.MapPath(“Logs”) + \\MylogFile.log);

string[] headers = textData[0].Split(‘,’);

 

//Create and populate DataTable

DataTable dataTable1 = newDataTable();

foreach (string header in headers)

dataTable1.Columns.Add(header, typeof(string), null);

 

for (int i = textData.Length – 1; i > 0; i–)

dataTable1.Rows.Add(textData[i].Split(‘,’));

 

//Set the DataSource of DataGridView to the DataTable

GridView1.DataSource = dataTable1;

GridView1.DataBind();

 

 

Permanent link to this article: http://www.techxpress.in/create-gridview-from-csv-file-in-c-sharp-c/

Bookmark this page