1 Replies - 243 Views - Last Post: 19 June 2019 - 08:12 AM Rate Topic: -----

#1 Cheddar0811   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 31-July 14

Datagridview stops inserting data when I add directory loop

Posted 19 June 2019 - 06:51 AM

I have an app that is looping through a directory folder and taking csv file data and placing it into a datagridview. I am using a text field parser to do this. My problem is when I save all of the file names into an array and loop through the array the code stops writing the information into the datagridview. The error seems to be coming from this line specifically:

Using myReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(element)


If I exchange the word element with the full pathway in "" marks the code runs fine. The datagridview has all the data written into it. However, there are over 100 files in this directory and I want it to automate stepping through them. Here is all my code.

  Dim i As Integer = 0
            Dim Files() As String = Directory.GetFiles("Y:\Public\Sanmina Production\3-Projector MTF Test\", "*.csv", SearchOption.TopDirectoryOnly)
            Dim sResult As String = ""
            For Each element As String In Files
                sResult &= element & " "
            Next
            MsgBox(sResult)

            For Each element As String In Files

                Try

                    'Y:\Public\Sanmina Production\3-Projector MTF Test\MTFTest_LJ902A180412AT00022_2019-04-10_17-14-11.csv


                    i = 0
                    Using myReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(element)
                        myReader.TextFieldType = FileIO.FieldType.Delimited
                        myReader.SetDelimiters(",")

                        Dim currentRow As String()
                        While Not myReader.EndOfData
                            Try
                                currentRow = myReader.ReadFields()
                                Dim currentField As String
                                For Each currentField In currentRow
                                    If currentField = "" Then
                                        'Must be value
                                        DataGridView1.Rows(0).Cells(i).Value = currentField
                                        i = i + 1
                                    Else

                                        If currentField.Substring(0, 1) = "M" Then

                                            If currentField.Substring(1, 1) = "T" Then

                                                If currentField.Substring(2, 1) = "F" Then
                                                    'Skip this one
                                                    ' MsgBox("Skip")
                                                Else
                                                    DataGridView1.Rows(0).Cells(i).Value = currentField
                                                    i = i + 1
                                                End If
                                            Else
                                                DataGridView1.Rows(0).Cells(i).Value = currentField
                                                i = i + 1

                                            End If
                                        Else
                                            DataGridView1.Rows(0).Cells(i).Value = currentField
                                            i = i + 1

                                        End If
                                    End If

                                Next
                            Catch ex As Microsoft.VisualBasic.
                            FileIO.MalformedLineException
                                MsgBox("Line " & ex.Message &
                    "is not valid and will be skipped.")
                            End Try
                        End While
                    End Using

                    DataGridView1.Rows.Add("")
                    i = 0


                Catch ex As Exception
                    MsgBox(ex.ToString)
                End Try

            Next

            '    strFileName = Dir()
            'Loop



I have added watches to make sure all my variables are correct and they are. However all the
DataGridView1.Rows(0).Cells(i).Value = currentField

stops working once I include the "element" variable. If I replace the "element" variable with the full string pathway the code runs fine. I don't understand why adding the array loop is causing an issue.

Is This A Good Question/Topic? 0
  • +

Replies To: Datagridview stops inserting data when I add directory loop

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15114
  • View blog
  • Posts: 60,481
  • Joined: 12-June 08

Re: Datagridview stops inserting data when I add directory loop

Posted 19 June 2019 - 08:12 AM

Quote

If I exchange the word element with the full pathway in "" marks the code runs fine.

I don't understand what you are referring to. Please clarify.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1