Search code examples
vb.netdatagridviewstreamreaderstreamwriter

Writing and Reading DataGridView to text file


I'm looking for some assistance on how to populate a DataGridView control with data from a text file. The column number is static, but the row count is dynamic. I am using StreamWriter to create the text file line by line. Here is the code:

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

The output file looks like this:

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

I'm stumped when using the StreamReader method to read each line from the text file and place it in the correct column and row. I had success populating text boxes using the "|" delimited format, but I'm not sure how to go about tackling the DataGridView code. Would it be wise to reformat my StreamWriter method to make the file comma delimited instead of "|" delimited?

UPDATE 10/08/2017

Ok, I have the writer formatting the text file the way I want and the StreamReader is 95% working. It successfully populates the DataGridView for the first row, but it needs to recognize every line beginning with "Labor" and add a new row with the array in the line. Here is a sample of the text I'm trying to read into the 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

Here is the StreamReader code:

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

The part I'm having trouble with is the number of lines beginning with "Labor" will vary every time the text file is written, so I think I need a Do Until or Do While loop within the If String.Equals method, but I cannot get the syntax right. I've tried these so far with no success:

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

Any ideas?

UPDATE WITH SOLUTION 10/09/2017

After really thinking about what my code is doing, I finally solved the problem. It's stupid simple and proves how much I was overthinking everything. Thanks for your assistance and patience with me, jmcilhinney. Hope this can help someone else out as well.

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

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

        End If

Solution

  • After really thinking about what my code is doing, I finally solved the problem. It's stupid simple and proves how much I was overthinking everything. Thanks for your assistance and patience with me, jmcilhinney. Hope this can help someone else out as well.

            ElseIf String.Equals(prop(0), "Labor") Then
    
                dgvLabor.Rows.Add(prop(1), prop(2), prop(3), prop(4), prop(5), prop(6))
    
            End If
    

    This code takes the already split line and inserts the desired objects of the array into the next available row's cells. It only adds the lines in the text document that begin with "Labor"