Private Function CreateVenueDataTable() As DataTable
Dim str As String = "C:\BA\ChartData\ChartData.txt"
Dim list As List(Of String) = New List(Of String)()
'Create a DataTable as the data source of the Chart control
Dim dt As New DataTable
dt.Columns.AddRange({New DataColumn("Venue", GetType(String)), New DataColumn("Profit", GetType(Decimal))})
If (System.IO.File.Exists(str)) Then
list.Clear()
list.AddRange(System.IO.File.ReadAllLines(str))
Dim profit As Decimal = 0
Dim dict As New Dictionary(Of String, Decimal)
For i As Integer = 0 To list.Count - 1
Dim parts As String() = list(i).Split(New Char() {";"c})
Dim valueToSearch As String = parts(2)
profit = parts(5)
If dict.ContainsKey(valueToSearch) Then
dict(valueToSearch) += profit
Else
dict.Add(valueToSearch, profit)
End If
Next
For Each key As String In dict.Keys
Dim dr As DataRow
'Add rows to the table which contains some random data for demonstration
dr = dt.NewRow()
dr("Venue") = key
dr("Profit") = dict(key)
dt.Rows.Add(dr)
Next
End If
Return dt
End Function
Maybe the file won't get very big but going through it several times for each line can get very slow. Dictionary already has the ContainsKey out of the box.
Private Function CreateVenueDataTable() As DataTable
Dim str As String = "C:\BA\ChartData\ChartData.txt"
Dim list As List(Of String) = New List(Of String)()
'Create a DataTable as the data source of the Chart control
Dim dt As New DataTable
dt.Columns.AddRange({New DataColumn("Venue", GetType(String)), New DataColumn("Profit", GetType(Decimal))})
If (System.IO.File.Exists(str)) Then
list.Clear()
list.AddRange(System.IO.File.ReadAllLines(str))
Dim profit As Decimal = 0
For i As Integer = 0 To list.Count - 1
Dim parts As String() = list(i).Split(New Char() {";"c})
Dim valueToSearch As String = parts(2)
profit = parts(5)
Dim drs() As DataRow = dt.Select("Venue = '" & valueToSearch.Replace("'", "''") & "'")
If drs.Length > 0 Then
drs(0)("Profit") += profit
Else
Dim dr As DataRow
'Add rows to the table which contains some random data for demonstration
dr = dt.NewRow()
dr("Venue") = valueToSearch
dr("Profit") = profit
dt.Rows.Add(dr)
End If
Next
End If
Return dt
End Function
I couldn't get the code to display red and blue
Here is the code I am using.
Any help would be great
Open in new window