【问题标题】:Writing and Reading DataGridView to text file将 DataGridView 写入和读取到文本文件
【发布时间】:2018-03-07 04:45:11
【问题描述】:

我正在寻求有关如何使用文本文件中的数据填充 DataGridView 控件的帮助。列数是静态的,但行数是动态的。我正在使用 StreamWriter 逐行创建文本文件。代码如下:

Private Sub btnSaveReport_Click(sender As Object, e As EventArgs) Handles btnSaveReport.Click
    Dim path As String = "c:\users\service5\desktop\ReportFile.txt"

    Using writer As New StreamWriter(path)

        For row As Integer = 0 To dgvLabor.RowCount - 2
            For col As Integer = 0 To dgvLabor.ColumnCount - 1
                writer.WriteLine("Labor" & "|" & dgvLabor.Rows(row).Cells(col).Value)
            Next
        Next

        writer.Close()
    End Using
End Sub

输出文件如下所示:

Labor|Mon 09/25/2017
Labor|Labor Time
Labor|11:42 PM
Labor|12:42 AM
Labor|23.00
Labor|20

当我使用 StreamReader 方法从文本文件中读取每一行并将其放置在正确的列和行中时,我感到很困惑。我使用“|”成功填充了文本框分隔格式,但我不确定如何处理 DataGridView 代码。重新格式化我的 StreamWriter 方法以使文件以逗号分隔而不是“|”是否明智?分隔?

2017 年 10 月 8 日更新

好的,我让编写器按照我想要的方式格式化文本文件,并且 StreamReader 的工作率为 95%。它成功地为第一行填充了 DataGridView,但它需要识别以“Labor”开头的每一行,并在该行中添加一个包含数组的新行。这是我试图读入 DataGridView 的文本示例。

Labor,Sun 10/08/2017,Labor Time,12:39 AM,12:39 AM,0.00,0
Labor,Mon 10/09/2017,Travel Time,12:39 AM,12:39 AM,0.00,0

这是 StreamReader 代码:

Private Sub OpenFileDialog1_FileOk(sender As Object, e As CancelEventArgs) Handles OpenFileDialog1.FileOk

    Dim FileReader As StreamReader
    Dim prop(2) As String
    Dim path As String = OpenFileDialog1.FileName

    FileReader = New StreamReader(path, False)

    Do Until FileReader.EndOfStream

        prop = FileReader.ReadLine().Split(",")

    If String.Equals(prop(0), "Labor") Then

            Dim col(6) As String
            Dim index = dgvLabor.Rows.Add()
            col = FileReader.ReadLine().Split(",")
            dgvLabor.Rows(index).SetValues(col(1), col(2), col(3), col(4), col(5), col(6))

        End If
    Loop

    FileReader.Close()

End Sub

我遇到的问题是每次写入文本文件时,以“Labor”开头的行数都会有所不同,所以我认为我需要在 If String.Equals 方法中使用 Do Until 或 Do While 循环,但我无法正确使用语法。到目前为止,我已经尝试了这些但没有成功:

Do While String.Equals("Labor")
Do Until String.Equals(Not "Labor")
Do Until String.Equals(prop(0), "Labor")

有什么想法吗?

2017 年 10 月 9 日更新解决方案

在认真思考了我的代码在做什么之后,我终于解决了这个问题。这是愚蠢的简单,证明了我是多么想太多。感谢您对我的帮助和耐心,jmcilhinney。希望这也可以帮助其他人。

        ElseIf String.Equals(prop(0), "Labor") Then

            dgvLabor.Rows.Add(prop(1), prop(2), prop(3), prop(4), prop(5), prop(6))

        End If

【问题讨论】:

  • 当您读取文件时网格是否会开始为空,或者是否已经填充并且您想要更新现有数据?
  • 读取文件时网格将开始为空。
  • 在这种情况下,不会在正确的行中放置任何内容,因为没有行开头。您将使用DoWhile 循环来读取数据,并在每次迭代中创建一个新行,填充它,然后将其添加到网格中。我很难相信您找不到从网络上的 CSV 文件填充 DataGridView 的示例。您的分隔符不是逗号这一事实基本上是无关紧要的,因为您在读取数据时只需按照您喜欢的任何字符进行拆分。
  • 我在网上找到了一些示例,但不确定它们是否适用于我的应用程序。我不知道如果网格为空,编码会变得更简单。
  • 在线示例应该可以工作,除非您的情况有一些具体的相关差异。如果有,我们可以在您遇到特定问题时为您提供帮助。

标签: vb.net datagridview streamreader streamwriter


【解决方案1】:

在认真思考了我的代码在做什么之后,我终于解决了这个问题。这是愚蠢的简单,证明了我是多么想太多。感谢您对我的帮助和耐心,jmcilhinney。希望这也可以帮助其他人。

        ElseIf String.Equals(prop(0), "Labor") Then

            dgvLabor.Rows.Add(prop(1), prop(2), prop(3), prop(4), prop(5), prop(6))

        End If

此代码采用已拆分的行并将所需的数组对象插入到下一个可用行的单元格中。它只在文本文档中添加以“Labor”开头的行

【讨论】:

    猜你喜欢
    • 2016-10-06
    • 1970-01-01
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 2016-09-04
    • 1970-01-01
    • 1970-01-01
    • 2021-04-29
    相关资源
    最近更新 更多