Hello, first of all I am new to vb.net so sorry if my question is stupid... :(

I have text files that are something like this:

"#......
"#......
1.2 2.5
3.4 5.6
4.8 8.9
.
.
.

Now I would like to add to the first column of the datagridview the first column of the text file (1.2, 3.4, 4.8) and on the second column of the dgv the second column of the text. But without importing the first lines with #....

my code so far is something like this:

Using stream As System.IO.FileStream = System.IO.File.OpenRead("FilePath")
            Using reader As New System.IO.StreamReader(stream)

                Dim line As String = reader.ReadLine()
                Dim FirstCharacter As String = reader.ReadLine(0)
                Dim lines As String() = IO.File.ReadAllLines("FilePath")

                MsgBox(FirstCharacter)
                MsgBox(line)
                MsgBox(lines.Length)


                While line IsNot Nothing
                    Dim columns = line.Split(" ")
                    line = reader.ReadLine()
                    Dim index = Me.DataGridView1.Rows.Add()
                    Me.DataGridView1.Rows(index).SetValues(columns)

                End While

            End Using
        End Using

Thank you very much in advance.. :)

Recommended Answers

How about

For Each line As String In lines
    if not line.StartsWith("#") then
        Dim columns() As String = line.Split(" ")
        Dim index = Me.DataGridView1.Rows.Add()
        Me.DataGridView1.Rows(index).SetValues(columns)
    end if
Next
Jump to Post

All 2 Replies

Re: Import Text without Header into a DataGridView 80 80

How about

For Each line As String In lines
    if not line.StartsWith("#") then
        Dim columns() As String = line.Split(" ")
        Dim index = Me.DataGridView1.Rows.Add()
        Me.DataGridView1.Rows(index).SetValues(columns)
    end if
Next
Re: Import Text without Header into a DataGridView 80 80

Thank you very much, that is really close to what I want.

It seems that it only takes into account one line with # but at the end it throws an exception "An unhandled exception of type 'System.NullReferenceException'".

Edit: Oops Sorry, I didn't see that you chnged while with for... It is working! Thank you!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.