VS 2013 partial .txt file to datagridview using vb.net-VBForums
Results 1 to 16 of 16

Thread: partial .txt file to datagridview using vb.net

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2016
    Posts
    40

    partial .txt file to datagridview using vb.net

    i have a user interface form that let's you upload a text file to a datagridview as follows

    Sub Datagrid()
    Dim sw = System.Diagnostics.Stopwatch.StartNew()
    Using stream
    As System.IO.FileStream = System.IO.File.OpenRead(TextBox1.Text)
    Using reader
    AsNew System.IO.StreamReader(stream)
    Dim line AsString= reader.ReadLine()
    While(line IsNot Nothing)
    Dim columns = line.Split(";")
    line
    = reader.ReadLine()
    Dim index =Me.DataGridView1.Rows.Add()
    Me.DataGridView1.Rows(index).SetValues(columns)
    EndWhile
    End Using
    End Using
    sw
    .Stop()
    EndSub

    Well, now my problem is that i don't want to put the full txt file in that datagridview, just from line N. Is it possible to do that? Like creating a querytabel and selecting a fixed value?

    p.e., in line 5 there's always the text "Values:" . Can i select all the lines after that to put in the datagridview?
    i googled everywhere but found nothing. and there's no "sample" code to give me a start .

    thank you all !

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,616

    Re: partial .txt file to datagridview using vb.net

    If what you want in the data grid is everything from line 5 and after, then read the entire file into a lines() array and use a for:Next loop...

    Code:
    dim lines() as string = io.file.readAllLines(filePath)
    For lineIndex as integer = 5 to lines.count-1
         dim line as string = lines(lineIndex)
        'import the line
    Next
    you should also turn on Option Strict and declare all of your variables.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2016
    Posts
    40

    Re: partial .txt file to datagridview using vb.net

    oh nice, thank you!

    thinking forward, how can i put lines 1 to 5 in datagridview1 (and 6 till the end in datagridview2) ?

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,202

    Re: partial .txt file to datagridview using vb.net

    Take a look at this example:
    Code:
    Dim n As Integer = 5
    Dim lines As IEnumerable(Of String) = IO.File.ReadAllLines("my_file.txt").Skip(n) 'Gets every line after a certain line count
    
    'Create a new datatable and add some columns
    Dim dt As New DataTable
    dt.Columns.AddRange((From column As String In lines.First.Split(";"c) Select New DataColumn(column)).ToArray())
    
    'Add each line as a row to the datatable
    For Each line As String In lines
        dt.Rows.Add(line.Split(";"c))
    Next
    
    'Set the datasource of the datagridview
    MyDataGridView.DataSource = dt
    Edit - I just noticed that you want to skip some lines too. You'd adjust the lines variable to:
    Code:
    Dim lines As IEnumerable(Of String) = IO.File.ReadAllLines("my_file.txt").Take(n) 'Gets every line up to a certain line count
    If you wanted to get lines 2 - 5 then you'd do:
    Code:
    Dim lines As IEnumerable(Of String) = IO.File.ReadAllLines("my_file.txt").Skip(1).Take(4) 'Gets every line between two values
    Last edited by dday9; Feb 11th, 2016 at 01:10 PM.

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2016
    Posts
    40

    Re: partial .txt file to datagridview using vb.net

    Quote Originally Posted by dday9 View Post
    Take a look at this example:
    Code:
    Dim n As Integer = 5
    Dim lines As IEnumerable(Of String) = IO.File.ReadAllLines("my_file.txt").Skip(n) 'Gets every line after a certain line count
    
    'Create a new datatable and add some columns
    Dim dt As New DataTable
    dt.Columns.AddRange((From column As String In lines.First.Split(";"c) Select New DataColumn(column)).ToArray())
    
    'Add each line as a row to the datatable
    For Each line As String In lines
        dt.Rows.Add(line.Split(";"c))
    Next
    
    'Set the datasource of the datagridview
    MyDataGridView.DataSource = dt
    Edit - I just noticed that you want to skip some lines too. You'd adjust the lines variable to:
    Code:
    Dim lines As IEnumerable(Of String) = IO.File.ReadAllLines("my_file.txt").Take(n) 'Gets every line up to a certain line count
    If you wanted to get lines 2 - 5 then you'd do:
    Code:
    Dim lines As IEnumerable(Of String) = IO.File.ReadAllLines("my_file.txt").Skip(1).Take(4) 'Gets every line up to a certain line count
    That answers my question ! thank you guys ! cumps

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,202

    Re: partial .txt file to datagridview using vb.net

    I noticed something in my code, change this line:
    Code:
    dt.Columns.AddRange((From column As String In lines.First.Split(";"c) Select New DataColumn(column)).ToArray())
    To this:
    Code:
    dt.Columns.AddRange((From columnIndex As Integer In Enumerable.Range(1, lines.First.Split(";"c).Count) Select New DataColumn("Column" & columnIndex.ToString())).ToArray())
    The adjustment will name the columns: Column1, Column2, etc. rather than naming them the respective values in the first row.
    Last edited by dday9; Feb 11th, 2016 at 01:27 PM. Reason: changed column to row

  7. #7

    Thread Starter
    Member
    Join Date
    Feb 2016
    Posts
    40

    Re: partial .txt file to datagridview using vb.net

    Quote Originally Posted by dday9 View Post
    I noticed something in my code, change this line:
    Code:
    dt.Columns.AddRange((From column As String In lines.First.Split(";"c) Select New DataColumn(column)).ToArray())
    To this:
    Code:
    dt.Columns.AddRange((From columnIndex As Integer In Enumerable.Range(1, lines.First.Split(";"c).Count) Select New DataColumn("Column" & columnIndex.ToString())).ToArray())
    The adjustment will name the columns: Column1, Column2, etc. rather than naming them the respective values in the first row.
    the way u are doing that, each line creates a new column instead of a new row.
    i'm trying to adapt the code now .

    edit2: i can't make it to write in the cells, not in the headers ( i already have the headers) and when i changed the code, i added the exact number of rows, but without any values in the cells..
    Last edited by iDio; Feb 12th, 2016 at 07:24 AM.

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,202

    Re: partial .txt file to datagridview using vb.net

    If you have the column headers already then just skip that line but keep the rest of the code.

  9. #9

    Thread Starter
    Member
    Join Date
    Feb 2016
    Posts
    40

    Re: partial .txt file to datagridview using vb.net

    Quote Originally Posted by dday9 View Post
    If you have the column headers already then just skip that line but keep the rest of the code.
    already did that, of course. it still gives me an error..

    Dim n As Integer = 5
    Dim lines As IEnumerable(Of String) = IO.File.ReadAllLines(TextBox1.Text).Skip(n)
    Dim dt As New DataTable

    ' dt.Columns.AddRange((From column As String In lines.First.Split(";"c) Select New DataColumn(column)).ToArray())

    For Each line As String In lines
    dt.Rows.Add() '(line.Split(";"c))
    Next

    DataGridView2.DataSource = dt

    this is the only way to make it "kinda" work. it adds the rows, but adds no values. if i put the full code it gives me an error in the split method..

  10. #10
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,202

    Re: partial .txt file to datagridview using vb.net

    How does each line separate it's cells? I thought that the layout of the text file was like this:
    Code:
    1;2;3;4;5
    6;7;8;9;10
    11;12;13;14;15
    Is it in a different format?

  11. #11

    Thread Starter
    Member
    Join Date
    Feb 2016
    Posts
    40

    Re: partial .txt file to datagridview using vb.net

    Quote Originally Posted by dday9 View Post
    How does each line separate it's cells? I thought that the layout of the text file was like this:
    Code:
    1;2;3;4;5
    6;7;8;9;10
    11;12;13;14;15
    Is it in a different format?
    nope. just like that !

    edit: i'll try to translate_
    System.ArgumentException: The input matrix is longer than the number of columns in this table. ( and it isn't!! format: "1;13:01:22;3,8;OK" )
    Last edited by iDio; Feb 12th, 2016 at 09:58 AM.

  12. #12
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,202

    Re: partial .txt file to datagridview using vb.net

    When I test my code on declared string with that content I get the desired results. This is what I tested on a console application:
    Code:
    Imports System
    Imports System.Collections.Generic
    Imports System.Data
    Imports System.Linq
    Imports System.Xml
    Public Module Module1
    	Public Sub Main()
    		Dim n As Integer = 5
    		'Dim lines As IEnumerable(Of String) = IO.File.ReadAllLines("my_file.txt").Skip(n) 'Gets every line after a certain line count
    		Dim lines() As String = {"1;2;3;4;5", "6;7;8;9;10", "11;12;13;14;15"}
    		
    		'Create a new datatable and add some columns
    		Dim dt As New DataTable
    		dt.Columns.AddRange((From columnIndex As Integer In Enumerable.Range(1, lines.First.Split(";"c).Count) Select New DataColumn("Column" & columnIndex.ToString())).ToArray())
    		
    		'Add each line as a row to the datatable
    		For Each line As String In lines
    			dt.Rows.Add(line.Split(";"c))
    		Next
    		
    		'Set the datasource of the datagridview
    		'MyDataGridView.DataSource = dt
    		For Each row As DataRow In dt.Rows
    			Console.WriteLine(String.Join(" | ", (From c As DataColumn In dt.Columns Select row.Item(c)).ToArray()))
    		Next
    	End Sub
    End Module
    Fiddle: https://dotnetfiddle.net/1khNDC

  13. #13

    Thread Starter
    Member
    Join Date
    Feb 2016
    Posts
    40

    Re: partial .txt file to datagridview using vb.net

    i did that now and it worked. It created new columns and added the values like expected.

    but then i removed the code to add new columns and it didn't work ( with the same error)

  14. #14
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,202

    Re: partial .txt file to datagridview using vb.net

    Wait, are you trying to add columns to the DataGridView manually like this:
    Code:
    MyDatagridView.Columns.Add(....
    If so then you can't do that with my code. The reason for this is because we're setting the DataSource of the DataGridView:
    Code:
    MyDataGridView.DataSource = dt

  15. #15

    Thread Starter
    Member
    Join Date
    Feb 2016
    Posts
    40

    Re: partial .txt file to datagridview using vb.net

    Quote Originally Posted by dday9 View Post
    Wait, are you trying to add columns to the DataGridView manually like this:
    Code:
    MyDatagridView.Columns.Add(....
    If so then you can't do that with my code. The reason for this is because we're setting the DataSource of the DataGridView:
    Code:
    MyDataGridView.DataSource = dt
    is there any other way to change the column name? lol
    anyway, u solved my problem. thank you! i really don't care about the headers cumps

  16. #16
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,202

    Re: partial .txt file to datagridview using vb.net

    Yeah, if you want the column names to be different then change them in the DataTable rather than in the DataGridView. Or if you want column names to remain the same but the header text to be different then change the column's caption in the DataTable.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.