Hi indradeo,
Please refer the below code.
HTML
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
NameSpace
using System.Data;
using System.IO;
Code
protected void Page_Load(object sender, EventArgs e)
{
string filePath = @"D:\Test.csv";
string csvData = " ";
using (var stream = File.Open(filePath, FileMode.Open))
{
using (var reader = new StreamReader(stream))
{
//Read the contents of CSV file.
csvData = reader.ReadToEnd();
}
}
//Create a DataTable.
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[]
{
new DataColumn("UOM", typeof(string)),
new DataColumn("PM10", typeof(string)),
new DataColumn("PM2.5",typeof(string)),
new DataColumn("S02", typeof(string)),
new DataColumn("NO", typeof(string)),
new DataColumn("NO2", typeof(string)),
new DataColumn("NOx", typeof(string)),
new DataColumn("CO2", typeof(string)),
new DataColumn("TEMP" ,typeof(string)),
new DataColumn("WD", typeof(string)),
new DataColumn("WS", typeof(string)),
new DataColumn("RH",typeof(string)),
new DataColumn("SR", typeof(string)),
new DataColumn("rain_gauge",typeof(string))
});
//Execute a loop over the rows.
bool firstRow = true;
foreach (string row in csvData.Split('\n'))
{
if (firstRow)
{
firstRow = false;
}
else
{
if (!string.IsNullOrEmpty(row))
{
int i = 0;
DataRow dr = dt.NewRow();
foreach (string cell in row.Split(','))
{
dr[i] = cell;
i++;
}
dt.Rows.Add(dr);
}
}
}
//Bind the DataTable.
GridView1.DataSource = dt;
GridView1.DataBind();
}