10 Replies - 7010 Views - Last Post: 27 March 2011 - 09:41 AM Rate Topic: -----

#1 giovetti  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 25-February 11

read every n lines from text file

Posted 25 February 2011 - 07:53 PM

How can I read every eight lines of a text file, store it in arrays to load into a datagrid?
Is This A Good Question/Topic? 0
  • +

Replies To: read every n lines from text file

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: read every n lines from text file

Posted 25 February 2011 - 08:05 PM

How are you reading the text file? Show us the code you're trying. There are numerous ways of doing what you want.
Was This Post Helpful? 0
  • +
  • -

#3 Bra1nDead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-February 11

Re: read every n lines from text file

Posted 25 February 2011 - 11:06 PM

The easiest way that I know to do this would be using LINQ. I don't really know LINQ too well but my teacher gave my class a piece of code yesterday that should do what you want.

Dim myQuery = _
     From tmp in File.ReadAllLines("filename.txt") _ 'selects the text file you want to use and breaks it up into each line
     Select tmp 'this will place each line into the myQuery collection


That should break each line of the text file into the collection.
I hope I'm not doing your homework for you but if I am, I hope you get an A for this.
Was This Post Helpful? 0
  • +
  • -

#4 giovetti  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 25-February 11

Re: read every n lines from text file

Posted 27 February 2011 - 01:58 AM

Hi guys thanks for the replies, I am no expert and I am building this from my VB.NET beginner knowledge with the google and few books knowledge. Below is the code I got from internet and am using to read the file.The file is a text file with one column only. I want to write 6 lines on the begging of the text file first, then read the text file eight lines at time and load the eight lines into column of the datagridview, 12 columns in total and if the file lines exceeds the 12 columns then I will send the full datagridview to a specific range of cells in excel and print it and do it all again with the remaining lines.
I think that I will need to delete the read lines so when I come back to read the text file again I do not read the already read lines.
I know it is a load of work but appreciate the time you spending reading and commenting. Thanks a lot.

Private Sub ButtonOpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOpenFile.Click
        Dim fName As String = ""

        'Open the file
        OpenFileDialog1.InitialDirectory = "F:\STBRL\MyDocsSyphPC" 'Scooby download directory

        OpenFileDialog1.Filter = "CSV files (*.csv)|*.CSV"

        OpenFileDialog1.FilterIndex = 2

        OpenFileDialog1.RestoreDirectory = True

        If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then

            fName = OpenFileDialog1.FileName

        End If

        Me.TextBoxOpenFilePath.Text = fName

        Dim TextLine As String = ""

        Dim SplitLine() As String



        If System.IO.File.Exists(fName) = True Then

            Dim objReader As New System.IO.StreamReader(fName)

            Do While objReader.Peek() <> -1

                TextLine = objReader.ReadLine()

                SplitLine = Split(TextLine, ",")

                Me.DataGridView1.Rows.Add(SplitLine)

            Loop


        Else

            MsgBox("File Does Not Exist")

        End If


        ' Resize the height of the column headers. 
        DataGridView1.AutoResizeColumnHeadersHeight()

        DataGridView1.AutoSizeColumnsMode = _
            DataGridViewAutoSizeColumnsMode.AllCells


    End Sub


Was This Post Helpful? 0
  • +
  • -

#5 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1053
  • View blog
  • Posts: 4,080
  • Joined: 02-July 08

Re: read every n lines from text file

Posted 27 February 2011 - 05:09 AM

Don't forget to close your file streams. Here's how I load a CSV file into a DGV.
Private dt As New DataTable
'add the columns you need
dt.Columns.Add("name")

'then load it
If File.Exist(<path>) Then
   Using sr As New StreamReader(<path>)
     While Not sr.EndOfSTream
       dt.Rows.Add(sr.ReadLine.Split(","c))
     End While
   End Using 'stream closed and disposed
End If

DGV.DataSource = dt

Was This Post Helpful? 1
  • +
  • -

#6 giovetti  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 25-February 11

Re: read every n lines from text file

Posted 03 March 2011 - 06:50 PM

hawkvalley thanks for that but what I realy want is a way to limit the stream being read from the text file by 8 lines, then input those 8 lines into the datagridview, read the next 8 lines from the same text file and then add the 8 lines into the next column and so on until the end of file, alternatively if I could do that to an excel file directly would be great
Was This Post Helpful? 0
  • +
  • -

#7 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1053
  • View blog
  • Posts: 4,080
  • Joined: 02-July 08

Re: read every n lines from text file

Posted 03 March 2011 - 07:11 PM

Reading from an excel file is done by Excel Automation or with the Microsoft jet engine. You can make an array of the data then you add the data to a datatable column, then display the datatable in the DGV.

Excel to Datatable
Was This Post Helpful? 0
  • +
  • -

#8 giovetti  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 25-February 11

Re: read every n lines from text file

Posted 04 March 2011 - 11:46 AM

Either I didn't explain myself weel or you must misread it.
What I want is to read from a text file, a plain text file with only one column of many lines.

I have the code to read the complete file at one and place it to one column of the datagridview.
What I want to be able to do is divide that columns in many 8 lines and be paste them into 12 or n number of columns in the datagridview.
I will then create another datatable or datagrid to populate with the file contents if it exceeds the 8x12 format

this is the only way I know to be able to paste the 8x12 organized grid into an existing excelsheet and print it.

I was wondering if there is a way to breack the file into eight lines to be able to do this
Was This Post Helpful? 0
  • +
  • -

#9 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1053
  • View blog
  • Posts: 4,080
  • Joined: 02-July 08

Re: read every n lines from text file

Posted 04 March 2011 - 03:18 PM

You said this:

View Postgiovetti, on 03 March 2011 - 08:50 PM, said:

alternatively if I could do that to an excel file directly would be great

Hence the answer I gave.

Use need to use some logic and a counter variable to help the way to import the lines.
Was This Post Helpful? 0
  • +
  • -

#10 Jeff H  Icon User is offline

  • D.I.C Regular

Reputation: 112
  • View blog
  • Posts: 308
  • Joined: 30-January 11

Re: read every n lines from text file

Posted 04 March 2011 - 04:36 PM

Why don't you use File.ReadAllLines which will put each line as a element in a string array.

Then if you divide using integer divison the array length by 8 you know how many sets of eight you have

Then grab eight elements etc.... or skip the first 6 etc..

This post has been edited by Jeff H: 04 March 2011 - 04:36 PM

Was This Post Helpful? 0
  • +
  • -

#11 giovetti  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 25-February 11

Re: read every n lines from text file

Posted 27 March 2011 - 09:41 AM

I was able to select every n element from the lines, selecting this way for example all elements on column 2 of my data.
I want to be able to organize or append few columns under one column.
for example:
my file comprises of this data separated by tab:

column1 column2 column3 column4
sampleResult sampleResult sampleID sampleID
78 8 1 4
78 98 2 5
7 09 3 6
98 7
87 8
87 9
87 10



I want to sort it like this

column1 column2
sampleID sampleResult
1 78
2 78
3 7
4 8
5 98
6 09
7 98
8 87
9 87
10 87

I tried put each columns into an array and then add the array into the datagridview, but as it reads as lines I get it mixed like for example column 1 and 2 into one column:

78
8
78
98
7
09

98

87

87

87

my code to read the file is below. I thought that by putting into an array it would be the column data only


 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'Open the file
        OpenFileDialog1.InitialDirectory = "F:\DTS RESULTS" 'Panther upload directory
        OpenFileDialog1.Filter = "CSV files (*.csv)|*.CSV"
        OpenFileDialog1.FilterIndex = 2
        OpenFileDialog1.RestoreDirectory = True

        If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            fName = OpenFileDialog1.FileName
        End If

        Me.TextBoxOpenFilePath.Text = fName

        If System.IO.File.Exists(fName) = True Then

            Dim currentrow(12) As String
            
            Try

                Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(fName)
                    MyReader.TextFieldType = FileIO.FieldType.Delimited
                    MyReader.SetDelimiters(vbTab)
                    While Not MyReader.EndOfData
                        currentrow = MyReader.ReadFields()
                        Dim col1 As String
                        Dim col2 As String
                        col1 = currentrow(1)
                        col2 = currentrow(2)

                        Dim Account(0 To 8) As String
                        Dim arrCol1() As String = {col1}
                        Dim srrCol2() As String = {col2}
                        
                        'DataGridView1.Rows.Add(currentrow(1))
                        'DataGridView1.Rows.Add(currentrow(2))

                    End While
                End Using

            Catch ex As Exception
                Throw ex
            End Try
        End If
    
  End sub



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1