Issues Converting DataType

pgm575

Member
Joined
Jul 16, 2010
Messages
12
Programming Experience
Beginner
I am attempting to fill a DataTable with columns of numbers from a CSV to create lines on a third party chart control called Zedgraph. The problem is, the control keeps plotting one-to-one lines of point counts (in other words, if the column has 400 points, it plots a straight line from 0,0 to 400,400). I believe it is doing this because the data in my table is still being input as type String, which it shouldn't given the Cdbl(Var(x)) function I have written in. Why do you think my table values are still of type String?

Imports ZedGraph

Public Class Form1

Private Sub CreateDataset(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim DataSet As New DataSet
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
DataSet = getCsvToDataset(OpenFileDialog1.FileName)
End If

Call ChartFill(DataSet)

End Sub

Private Sub ChartFill(ByVal DS As DataSet)
Dim ChartPane As GraphPane = ZG1.GraphPane
Dim dspl As New DataSourcePointList
With dspl
.DataSource = DS.Tables(0)
.XDataMember = "Time Slot"
.YDataMember = "X1 1000 Air"
End With

Dim line1 As CurveItem
line1 = ChartPane.AddCurve("X1 1000 Air", dspl, Color.Aquamarine)

ChartPane.AxisChange()

End Sub

Private Shared Function getCsvToDataset(ByVal strPath As String) As DataSet
Dim strLine As String
Dim strArray As String()
Dim charArray As Char() = New Char() {","c}
Dim ds As New DataSet()
Dim dt As DataTable = ds.Tables.Add("TheData")
Dim aFile As New IO.FileStream(strPath, IO.FileMode.Open)
Dim sr As New IO.StreamReader(aFile)

strLine = sr.ReadLine()
strArray = strLine.Split(charArray)

For x As Integer = 0 To strArray.GetUpperBound(0)
dt.Columns.Add(strArray(x).Trim())
Next

strLine = sr.ReadLine()
While strLine IsNot Nothing
strArray = strLine.Split(charArray)
Dim dr As DataRow = dt.NewRow()
For i As Integer = 0 To strArray.GetUpperBound(0)
dr(i) = strArray(i).Trim()
If dr(i) <> "" Then
dr(i) = CDbl(Val(dr(i)))
End If
Next
dt.Rows.Add(dr)
strLine = sr.ReadLine()
End While
sr.Close()

Dim ds2 As New DataSet
Dim dt2 As DataTable = ds2.Tables.Add("MoreData")


Return (ds)
End Function

End Class
 
Well I ended up fixing it, so here is my code if anyone else happens upon this problem. Also, thanks for the help JohnH.

VB.NET:
Imports ZedGraph

Public Class Form1

    Private Sub CreateDataset(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim DataSet As New DataSet
        Dim filePath As String
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            filePath = OpenFileDialog1.FileName
            DataSet = getCsvToDataset(filePath)
        Else
            MsgBox("You didn't select a data file.", vbOKOnly)
            Exit Sub
        End If

        Call ChartFill(DataSet)

    End Sub

    Private Sub ChartFill(ByVal DS As DataSet)
        Dim ChartPane As GraphPane = ZedGraphControl1.GraphPane
        Dim dspl As New DataSourcePointList
        Dim defX As String
        Dim defY As String

        defX = DS.Tables(0).Columns(0).ColumnName.ToString
        defY = DS.Tables(0).Columns(1).ColumnName.ToString

        With dspl
            .DataSource = DS.Tables(0)
            .XDataMember = defX
            .YDataMember = defY
        End With

        Dim line1 As CurveItem
        line1 = ChartPane.AddCurve(defY, dspl, Color.Black)

        ChartPane.AxisChange()

        chnlSelect.Visible = True
        For i As Integer = 0 To (DS.Tables(0).Columns().Count - 1)
            chnlSelect.Items.Add(DS.Tables(0).Columns(i).ColumnName)
        Next

    End Sub

    Private Shared Function getCsvToDataset(ByVal strPath As String) As DataSet
        Dim strLine As String
        Dim strArray As String()
        Dim charArray As Char() = New Char() {","c}
        Dim ds As New DataSet()
        Dim dt As DataTable = ds.Tables.Add("TheData")
        Dim aFile As New IO.FileStream(strPath, IO.FileMode.Open)
        Dim sr As New IO.StreamReader(aFile)

        strLine = sr.ReadLine()
        strArray = strLine.Split(charArray)

        For x As Integer = 0 To strArray.GetUpperBound(0)
            Dim dc As New DataColumn(strArray(x).Trim(), System.Type.GetType("System.Double"))
            dc.ColumnName = strArray(x).ToString
            dt.Columns.Add(dc)
        Next

        Dim style As System.Globalization.NumberStyles
        Dim culture As System.Globalization.CultureInfo
        style = Globalization.NumberStyles.Number
        culture = Globalization.CultureInfo.CreateSpecificCulture("en-GB")
        strLine = sr.ReadLine()
        While strLine IsNot Nothing
            strArray = strLine.Split(charArray)
            Dim dr As DataRow = dt.NewRow()
            Dim curval As Double
            For i As Integer = 0 To strArray.GetUpperBound(0)
                If strArray(i).Equals("") Then
                    dr(i) = "0.0"
                Else
                    Double.TryParse(strArray(i), style, culture, curval)
                    dr(i) = curval
                End If
            Next
            dt.Rows.Add(dr)
            strLine = sr.ReadLine()
        End While
        sr.Close()

        Return (ds)
    End Function

    Private Sub changeLine(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chnlSelect.SelectedIndexChanged
        '     Dim DS As New DataSet()
        '    DS = 
    End Sub
End Class
 
Back
Top