在 vb.net 中将多个文本文件添加到 gridview (devexpress)

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

我有一个包含多个文本文件的文件夹,每个文本文件大约有 14 行文本。 我想将该文件夹中的所有文本文件添加到 vb.net 中的 gridcontrol/gridview。

我当前的代码只添加了 1 个文本文件,而不是全部添加。任何帮助将不胜感激。

    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

没有必要显式声明 DataColumn 数组。它是从大括号中的项目内部创建的。

您在每次迭代中都在丢弃 NewItem。为什么不将它们添加到列表中并一次将它们全部添加到 ListView.

您在分配 LINE_ 数据时一遍又一遍地读取文件。阅读一次并使用生成的行数组。

您从未在此代码中使用 bc 所以我删除了它们

我使用 .net DataGridView 进行了测试。文件io方式和DataTable相同

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