Skip to content

Read and Display CSV data

Mahmud Choudhury

Asked Jan 12, 2015, 2:27 AM EST1 answer
Share
LinkedInX
Hiya;
I need to read and display data from a csv file in a table format. I then will need to use these data to calculate different values. This is how the CSV file looks:










9 carat gold11.87
18 carat gold23.73
Silver0.49
Platinum27.52


I need to display these on a table format on the webpage (able to do that as gridview) then use individual values from the table to work out calculation i.e 10 grams of silver should be worked out by multiplying 0.49 (retrieved from the table) by 10.

That's the code I have so far for GridView:

If File.Exists("metals.csv") Then
Dim data As String() = File.ReadAllLines("metals.csv")

Dim dt As New DataTable()
Dim col As String() = data(0).Split(",")
For Each s As String In col

dt.Columns.Add(s, GetType(String))

Next
For i As Integer = 0 To data.Length - 1

Dim row As String() = data(i).Split(",")

dt.Rows.Add(row)

Next

GridView1.DataSource = dt

GridView1.DataBind()
End If


Is there another way of doing this say by using arrays to assign variables for each metal. I need to make sure they are displayed in a table format.

Thank you

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.