Hi indradeo.
Please refer below code .
HTML
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
NameSpace
C#
using System.Data;
using System.IO;
VB.Net
Imports System.Data
Imports System.IO
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
//string filePath = @"C:\Users\Arjun\Desktop\Test.csv";
string filePath = @"C:\Users\Arjun\Desktop\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("Parameters", typeof(string)),
new DataColumn("PM10", typeof(string)),
new DataColumn("PM2.5",typeof(string)),
new DataColumn("SO2", 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)),
new DataColumn(" ",typeof(string)),
new DataColumn("",typeof(string))
});
// dt.Rows.Add("UoM");
int j = 0;
foreach (string row in csvData.Split('\n'))
{
if (j > 1)
{
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);
}
}
j++;
}
//Bind the DataTable.
GridView1.DataSource = dt;
GridView1.DataBind();
}
VB.Net
Protected Sub Page_Load(sender As System.Object, e As System.EventArgs) Handles Me.Load
Dim filePath As String = "C:\Users\Arjun\Desktop\Test.csv"
Dim csvData As String = " "
Using stream = File.Open(filePath, FileMode.Open)
Using reader = New StreamReader(stream)
csvData = reader.ReadToEnd()
End Using
End Using
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn() {
New DataColumn("Parameters"),
New DataColumn("PM10"),
New DataColumn("PM2.5"),
New DataColumn("SO2"),
New DataColumn("NO"),
New DataColumn("NO2"),
New DataColumn("NOx"),
New DataColumn("CO2"),
New DataColumn("Temp"),
New DataColumn("WD"),
New DataColumn("WS"),
New DataColumn("RH"),
New DataColumn("SR"),
New DataColumn("Rain_Gauge"),
New DataColumn(" "),
New DataColumn("")})
Dim j As Integer = 0
For Each row As String In csvData.Split(ControlChars.Lf)
If j > 1 Then
If Not String.IsNullOrEmpty(row) Then
Dim i As Integer = 0
Dim dr As DataRow = dt.NewRow
For Each cell As String In row.Split(","c)
dr(i) = cell
i = i + 1
Next
dt.Rows.Add(dr)
End If
End If
j = j + 1
Next
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
ScreenShot