Results 1 to 9 of 9

Thread: DataGridView and file txt

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2021
    Posts
    11

    DataGridView and file txt

    Hey,

    I would need help to use a SteamReader in order to read a file text and display some lines in a DataGridView.

    This file text looks like this

    Name:  Capture5.jpg
Views: 365
Size:  21.7 KB

    Columns of the DataGridView will be "nb / length / mass[g.mol] / Chain / Details"

    I'm reading the file line by line and trying to catch just those lines

    Name:  Capture6.PNG
Views: 300
Size:  5.9 KB

    Here is the code that i'm trying to do, I don't really know if using a StreamReader with a loop until the end of file is a good idea, and I'm not able to get only lines that I need.
    I will join the file text if someone can give me help.

    Code:
     Do Until reader.EndOfStream
                '  getLines = reader.ReadLine()
                If reader.ReadLine.Contains("Code") And reader.EndOfStream = False Then
                    reader.ReadLine()
                    reader.ReadLine()
                    test = reader.ReadLine.Substring(1, 11)
                Else
                    ' reader.ReadLine()
    
                End If
        dt_sum_protamex.Rows.Add(test)
            Loop
    Thank you.

    test.txt

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: DataGridView and file txt

    If you're going to write code to do something, you need to know EXACTLY what that thing is, or else there's no chance that the code will do it. You haven't told us what it is that you want to do so I have to wonder whether you actually know. "I want to read certain lines" is far too vague. What are the SPECIFIC, EXACT steps? If you were going to get a really stupid person to read the data in by hand from a written file, what instructions would you provide to them? If you don't know then you shouldn't be writing any code yet, because it is those instructions that constitute your algorithm and it is that algorithm that your code should be implementing. If you don't have an algorithm, you shouldn't be writing code yet. When you do have an algorithm, you always have something to compare your code to. Either the code implements the algorithm or it doesn't. If it doesn't then the fix is obvious: implement the algorithm. If the code does implement the algorithm and still doesn't work then it's the algorithm that's wrong, so you can go back and work on the algorithm some more. If you ever need help, you can tell us what you're algorithm is and point to the exact step that you're having trouble with. It doesn't take any programming experience to create the algorithm but beginners generally don't bother creating an algorithm and then blame their lack of programming experience for not being able to write the code.

  3. #3
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: DataGridView and file txt

    I would recommend using System.IO.File.ReadAllLines to read everything into a string array and then parse it instead of reading the file from a loop. I don't know exactly what you want to do but you can probably avoid a loop althogether using LINQ.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: DataGridView and file txt

    Quote Originally Posted by Peter Swinkels View Post
    I would recommend using System.IO.File.ReadAllLines to read everything into a string array and then parse it instead of reading the file from a loop. I don't know exactly what you want to do but you can probably avoid a loop althogether using LINQ.
    If you want all lines then ReadAllLines is a good option, as it reads all lines into an array. If you only want some of the lines, it's better to call ReadLines, which returns the data as an IEnumerable(Of String) but doesn't actually read a line until you use it. That way, you can discard the unwanted lines as you read them and not have them talking up room in an array. That's particularly good if the file is large but you only want a small number of the lines.

  5. #5
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: DataGridView and file txt

    You can actually use Linq to filter your array after reading it:

    Here's an example of how you could read an *.ini file and avoid having the comments in your list.
    Code:
    Imports System
    Imports System.Collections.Generic
    Imports System.IO
    Imports System.Linq
    
    Public Module Module1
       Public Sub Main()
          Dim Lines As New List(Of String)(From Line In File.ReadAllLines("C:\Windows\System.ini") Where Not Line.StartsWith(";"c))
    
          Lines.ForEach(AddressOf Console.WriteLine)
       End Sub
    End Module
    Unless the files are absolutely huge, I don't think memory is that big of an issue, especially if you immediately filter the data after having read it.
    Last edited by Peter Swinkels; Jun 14th, 2021 at 05:35 AM. Reason: Must learn to spell.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: DataGridView and file txt

    Quote Originally Posted by Peter Swinkels View Post
    You can actually use Linq to filter your array after reading it:

    Here's an example of how you could read an *.ini file and avoit having the comments in your list.
    Code:
    Imports System
    Imports System.Collections.Generic
    Imports System.IO
    Imports System.Linq
    
    Public Module Module1
       Public Sub Main()
          Dim Lines As New List(Of String)(From Line In File.ReadAllLines("C:\Windows\System.ini") Where Not Line.StartsWith(";"c))
    
          Lines.ForEach(AddressOf Console.WriteLine)
       End Sub
    End Module
    Unless the files are absolutely huge, I don't think memory is that big of an issue, especially if you immediately filter the data after having read it.
    That works alright, but it's still not ideal. If the file is small then it won't make that big a difference but that doesn't mean that you shouldn't do it a better way if one exists that is no more effort. That code first reads the whole file into an array, then creates an iterator on that array, then filters via that iterator and then loads the filtered data into a List. Why not create the iterator on the file and filter as you read, rather than reading everything from the file first, then reading all that data a second time to filter it and throw away the array that you didn't need in the first place? Rather than this:
    vb.net Code:
    1. Dim Lines As New List(Of String)(From Line In File.ReadAllLines("C:\Windows\System.ini") Where Not Line.StartsWith(";"c))
    do this:
    vb.net Code:
    1. Dim Lines = (From Line In File.ReadLines("C:\Windows\System.ini") Where Not Line.StartsWith(";"c)).ToList()
    I tend to prefer function syntax (which is more succinct in C#) but it amounts to the same thing:
    vb.net Code:
    1. Dim Lines = File.ReadLines("C:\Windows\System.ini").Where(Function(Line) Not Line.StartsWith(";"c)).ToList()
    The second option is objectively better. It doesn't make a big difference in many or even most cases, but why would you write code that you know is objectively worse when it takes no more effort to write better code? All else being equal, do it the right way. Only do it the wrong way if there is some specific advantage to doing so.

  7. #7
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: DataGridView and file txt

    The second option is objectively better. It doesn't make a big difference in many or even most cases, but why would you write code that you know is objectively worse when it takes no more effort to write better code? All else being equal, do it the right way. Only do it the wrong way if there is some specific advantage to doing so.
    - I wasn't aware of the flaw in the code I posted. Thank you for pointing it out.

  8. #8

    Thread Starter
    New Member
    Join Date
    Apr 2021
    Posts
    11

    Re: DataGridView and file txt

    Sorry about that, I wasn't clear enough.

    The DataGridView that I need to display will looks exactly like this :

    Code:
    "      "            nb  Lenght  mass[g/mol]  Chain  details
    Code:2 2
     Excellent 
     Good
     Moderate   
                        1    7    758.461  MAKLLAL       nb H = 0
                        2    9   1041.545  SLSFCFLLL       nb H = 0
                        3    3    235.051  GGC       nb H = 0
                        4    4    382.119  GGCF       nb H = 0
                        5   10   1211.574  ALREQPQQNE       nb H = 0
                        6    9    911.505  AGRALTVPQ       nb H = 0
    
     None  
    
    Code: 2 3
     None  
                     1    7    758.461  MAKLLAL       no letters H or C
                     2   10   1211.574  ALREQPQQNE       no letters H or C
                     3    9    911.505  AGRALTVPQ       no letters H or C
     Moderate 
                     1    9   1041.545  SLSFCFLLL       1 letter H or C
                     2    3    235.051  GGC       1 letter H or C
                     3    4    382.119  GGCF       1 letter H or C
     Good                
     Excellent
    As I said, I don't really have an idea how to handle that. I mean how to read only the lines that I need, and put them in a DataTable with a loop

    The txt file is in attachment in the first post, in that way you can see which lines are useless.
    I hope you can help me.

    Here is the code that i've now :

    Code:
            Dim number As String
            Dim testName As String
            Dim code As String
            Dim dt_sum_protamex As New DataTable
            dt_sum_protamex.Columns.Add(" ")
            dt_sum_protamex.Columns.Add("Number")
            dt_sum_protamex.Columns.Add("Length")
            dt_sum_protamex.Columns.Add("Mass [g/mol]")
            dt_sum_protamex.Columns.Add("Chain")
            dt_sum_protamex.Columns.Add("Details")
    
        
    
            Dim lineToRead As String
    
            Do Until reader.EndOfStream
                lineToRead = reader.ReadLine()
    
                If lineToRead.Contains("Code") Then
                    Label1.Text = lineToRead
                    code = lineToRead.Substring(1, 22).Trim
    
                    dt_sum_protamex.Rows.Add(code)
                    'reader.ReadLine()
                    reader.ReadLine()
                End If
                If lineToRead.Contains("Excellent") Then
                    testName = lineToRead.Substring(1, 10)
                    dt_sum_protamex.Rows.Add(testName)
                End If
                If lineToRead.Contains("Good") Then
                    testName = lineToRead.Substring(1, 6)
                    dt_sum_protamex.Rows.Add(testName)
    
                End If
                If lineToRead.Contains("Moderate") Then
                    testName = lineToRead.Substring(1, 9)
                    dt_sum_protamex.Rows.Add(testName)
    
                End If
                If lineToRead.Contains("None") Then
                    testName = lineToRead.Substring(1, 6)
                    dt_sum_protamex.Rows.Add(testName)
    
                End If
                If lineToRead.Length > 0 And lineToRead(0) = " " Then
                    number = lineToRead.Substring(6, 2)
                    dt_sum_protamex.Rows.Add(number)
                End If
    
            Loop
    Thank you
    Last edited by erataille; Jun 14th, 2021 at 07:43 AM.

  9. #9
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: DataGridView and file txt

    I'm confused. In your first post you said you only wanted these type of rows,

    1 7 758.461 MAKLLAL no letters H or C
    2 10 1211.574 ALREQPQQNE no letters H or C
    3 9 911.505 AGRALTVPQ no letters H or C
    Now you say you want the DGV to look like this,

    The DataGridView that I need to display will looks exactly like this :

    Code:
    " " nb Lenght mass[g/mol] Chain details
    Code:2 2
    Excellent
    Good
    Moderate
    1 7 758.461 MAKLLAL nb H = 0
    2 9 1041.545 SLSFCFLLL nb H = 0
    3 3 235.051 GGC nb H = 0
    4 4 382.119 GGCF nb H = 0
    5 10 1211.574 ALREQPQQNE nb H = 0
    6 9 911.505 AGRALTVPQ nb H = 0

    None

    Code: 2 3
    None
    1 7 758.461 MAKLLAL no letters H or C
    2 10 1211.574 ALREQPQQNE no letters H or C
    3 9 911.505 AGRALTVPQ no letters H or C
    Moderate
    1 9 1041.545 SLSFCFLLL 1 letter H or C
    2 3 235.051 GGC 1 letter H or C
    3 4 382.119 GGCF 1 letter H or C
    Good
    Excellent
    Which is it?

Posting Permissions

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



Click Here to Expand Forum to Full Width