1
Reply

Read and Display CSV data

Mahmud Choudhury

Mahmud Choudhury

Jan 12 2015 7:27 AM
716

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:
<table>
<tr>
<td>9 carat gold</td><td>11.87</td>
</tr><tr>
<td>18 carat gold</td><td>23.73</td>
</tr><tr>
<td>Silver</td><td>0.49</td>
</tr><tr>
<td>Platinum</td><td>27.52</td>
</tr>
</table>


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

Answers (1)