adding multiple text files to gridview (devexpress) in vb.net

290 views Asked by At

I have a folder with multiple text files in it, each text files has about 14 lines of text. I would like to add all text files in that folder to a gridcontrol/gridview in vb.net.

My current code only adds 1 text file instead of adding all. any help would be greatly appreciated.

    Dim path As String = "C:\Plan\"
    For Each i As String In System.IO.Directory.GetFiles(path)
        Dim a, b, c As String
        a = System.IO.Path.GetFileNameWithoutExtension(i)
        b = System.IO.Path.GetFileName(i)
        c = System.IO.Path.GetFullPath(i)

        Dim LINE_pair As String = IO.File.ReadLines(i).ElementAtOrDefault(0)
        Dim LINE1_details As String = IO.File.ReadLines(i).ElementAtOrDefault(1)
        Dim LINE2_outlookcombo As String = IO.File.ReadLines(i).ElementAtOrDefault(2)
        Dim LINE3_rsicombo As String = IO.File.ReadLines(i).ElementAtOrDefault(3)
        Dim LINE4_macdcombo As String = IO.File.ReadLines(i).ElementAtOrDefault(4)
        Dim LINE4_ratio As String = IO.File.ReadLines(i).ElementAtOrDefault(5)
        Dim LINE5_pattern As String = IO.File.ReadLines(i).ElementAtOrDefault(6)
        Dim LINE6_none As String = IO.File.ReadLines(i).ElementAtOrDefault(7)
        Dim LINE7_timeframecomvo As String = IO.File.ReadLines(i).ElementAtOrDefault(8)
        Dim LINE7_date As String = IO.File.ReadLines(i).ElementAtOrDefault(9)
        Dim LINE8_trade As String = IO.File.ReadLines(i).ElementAtOrDefault(10)
        Dim LINE9_currentprice As String = IO.File.ReadLines(i).ElementAtOrDefault(11)
        Dim LINE10_tp As String = IO.File.ReadLines(i).ElementAtOrDefault(12)
        Dim LINE11_sl As String = IO.File.ReadLines(i).ElementAtOrDefault(13)

        Dim NewItem As New ListViewItem(a)
        Dim dt As New DataTable()


        dt.Columns.AddRange(New DataColumn(13) {New DataColumn("Pair"), New DataColumn("Outlook"), New DataColumn("RSI"), New DataColumn("MACD"), New DataColumn("Pattern"), New DataColumn("Misc"), New DataColumn("Rato"), New DataColumn("Time Frame"), New DataColumn("Date"), New DataColumn("File name"), New DataColumn("Trade Status"), New DataColumn("CP"), New DataColumn("TP"), New DataColumn("SL")})
        dt.Rows.Add(New String() {LINE_pair, LINE2_outlookcombo, LINE3_rsicombo, LINE4_macdcombo, LINE5_pattern, LINE6_none, LINE4_ratio, LINE7_timeframecomvo, LINE7_date, a, LINE8_trade, LINE9_currentprice, LINE10_tp, LINE11_sl})

        GridControl1.DataSource = dt
1

There are 1 answers

0
Mary On BEST ANSWER

It is not necessary to declare the DataColumn array explicitly. It is created internally from the items in the braces.

You are throwing away your NewItem on each iteration. Why not add them to a list and add them all at once to a ListView.

You are reading the file over and over as you assign the LINE_ data. Read it once and use the resulting lines array.

You never use b and c in this code so I deleted them

I tested in with a .net DataGridView. The file io methods and the DataTable are the same.

Private Sub OPCode()
    Dim path As String = "C:\Plan\"
    Dim dt As New DataTable()
    Dim lstListViewItems As New List(Of ListViewItem)

    dt.Columns.AddRange({New DataColumn("Pair"), New DataColumn("Outlook"), New DataColumn("RSI"), New DataColumn("MACD"), New DataColumn("Pattern"), New DataColumn("Misc"), New DataColumn("Rato"), New DataColumn("Time Frame"), New DataColumn("Date"), New DataColumn("File name"), New DataColumn("Trade Status"), New DataColumn("CP"), New DataColumn("TP"), New DataColumn("SL")})
    For Each i As String In System.IO.Directory.GetFiles(path)
        Dim a = System.IO.Path.GetFileNameWithoutExtension(i)
        Dim lines = File.ReadLines(i)

        Dim LINE_pair As String = lines.ElementAtOrDefault(0)
        Dim LINE1_details As String = lines.ElementAtOrDefault(1)
        Dim LINE2_outlookcombo As String = lines.ElementAtOrDefault(2)
        Dim LINE3_rsicombo As String = lines.ElementAtOrDefault(3)
        Dim LINE4_macdcombo As String = lines.ElementAtOrDefault(4)
        Dim LINE4_ratio As String = lines.ElementAtOrDefault(5)
        Dim LINE5_pattern As String = lines.ElementAtOrDefault(6)
        Dim LINE6_none As String = lines.ElementAtOrDefault(7)
        Dim LINE7_timeframecomvo As String = lines.ElementAtOrDefault(8)
        Dim LINE7_date As String = lines.ElementAtOrDefault(9)
        Dim LINE8_trade As String = lines.ElementAtOrDefault(10)
        Dim LINE9_currentprice As String = lines.ElementAtOrDefault(11)
        Dim LINE10_tp As String = lines.ElementAtOrDefault(12)
        Dim LINE11_sl As String = lines.ElementAtOrDefault(13)
        dt.Rows.Add({LINE_pair, LINE2_outlookcombo, LINE3_rsicombo, LINE4_macdcombo, LINE5_pattern, LINE6_none, LINE4_ratio, LINE7_timeframecomvo, LINE7_date, a, LINE8_trade, LINE9_currentprice, LINE10_tp, LINE11_sl})
        Dim NewItem As New ListViewItem(a)
        lstListViewItems.Add(NewItem)
    Next

    ListView1.Items.AddRange(lstListViewItems.ToArray)

    DataGridView1.DataSource = dt
End Sub