Results 1 to 10 of 10

Thread: Create a DataTable from a txt file

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2021
    Posts
    11

    Create a DataTable from a txt file

    Hi, I want to create a DataTable in order to display it as DataGridView.

    Name:  Capture.jpg
Views: 363
Size:  18.0 KB

    Here is the text from the txt file that I want to display, I've no idea how to do it because the number of rows can change.
    I've this text in a string with a StreamReader

    If anyone has an idea how I could try to display this, it would be great !

    (Also ask me if I do not explain myself clearly)

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Create a DataTable from a txt file

    What does the text file look like? Those asterisks will be a nuisance, but for the body of the table, are those comma separated values or something else?
    My usual boring signature: Nothing

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

    Re: Create a DataTable from a txt file

    I've no idea how to do it because the number of rows can change
    The number of rows changing shouldn't be much trouble. Or did you mean the number of columns?

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

    Re: Create a DataTable from a txt file

    The number of rows is irrelevant, just like it is for a database. The issue is the specific layout and the columns. If it was just a standard delimited file or fixed-width file then you can use ADO.NET as though it were a database table. The next best option is a TextFieldParser and then a StreamReader. If the file actually looks as you have shown then a StreamReader may be your only option. If that's just your representation here for easy reading then you may have other options, but we need the actual format.

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2021
    Posts
    11

    Re: Create a DataTable from a txt file

    Hey, thank for your answers
    I also think that the best way is to use a StreamReader, but i don't really know how to loop on it to display the table as I need.
    Maybe i should create the columns "Name", "Total", ... and in the loop create rows that I need to put

    Attachment 181478

    The format is just a out file, so it like a txt file.

    There are no delimiters, only blanks

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

    Re: Create a DataTable from a txt file

    Quote Originally Posted by erataille View Post
    I also think that the best way is to use a StreamReader, but i don't really know how to loop on it to display the table as I need.
    That's because you haven't bothered to think about the logic before trying to write the code. What steps would you follow if you were performing the task manually? If you don't know, why don't you know? Those are the exact same steps that you need to follow in code, so that logic tells you what your code has to do. That's kind of important information, don't you think? It's fairly obvious what you would do manually:

    1. Read the first line of asterisks and discard it.
    2. Read the second line and either extract the column names if they may vary or discard and hard-code the column names if you know what they are.
    3. Read the third line of asterisks and discard it.
    4. Read each line in turn and either process it or discard and stop if it is all asterisks.

    It doesn't take any programming experience to work that out but now you have an algorithm to base your code on. That is what you should being doing EVERY time you need to write code.
    Quote Originally Posted by erataille View Post
    Maybe i should create the columns "Name", "Total", ... and in the loop create rows that I need to put
    Of course you should. You can't add any data to a DataTable that has no columns.
    Quote Originally Posted by erataille View Post
    The format is just a out file, so it like a txt file.

    There are no delimiters, only blanks
    It's not "just" anything. It is exactly what it is. If the first line is always asterisks, the second line is always column headers of specific widths, etc, etc, then that is what it is and you should be explaining that to us, not the other way around.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Create a DataTable from a txt file

    I don't think a StreamReader is the way to go, either. If the data is as poorly formatted as you suggest it is, you're going to have a hard time dealing with this by ANY means, but the place I would start would be one of these two:

    1) If you can get the data in a different format, do that. The current format is going to be miserable to work with, most likely.
    2) If the first option isn't viable, then look into ReadAllLines. I'd be sucking the entire file into an array of strings, because you're going to need to do some annoying processing on each line to figure out whether to use it or discard it, and if you decide it has data, to separate it into usable parts. Splitting on spaces might work for the latter, but only if those blanks you mention are spaces. Even then, it seems likely that the resulting pattern is going to turn out to be kind of irregular.
    My usual boring signature: Nothing

  8. #8
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Create a DataTable from a txt file

    It seems that the number of space is not the same for each line but the position of the first characters (for the first column) or of the last (for the numbers ) is always the same (to be confirmed!). So you will need to deal with the space around the + in the designation part and start at the last position and go backward to extract the numbers. I am pretty sure the regex experts here can do that easily (personally I will need more times...).

    I wonder if it would not be easier to first open the file in excel (that allows to set length to separate columns) and save it as a csv with coma separators and then use it in VB.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

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

    Re: Create a DataTable from a txt file

    Quote Originally Posted by Delaney View Post
    It seems...
    If only there was someone who was familiar with the file and could actually explain what is rather than what seems to be.
    Quote Originally Posted by Delaney View Post
    I wonder if it would not be easier to first open the file in excel (that allows to set length to separate columns) and save it as a csv with coma separators and then use it in VB.
    If you know what the fixed-width of the columns is then you can use a StreamReader and pull the field values out of a line in code.

  10. #10
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: Create a DataTable from a txt file

    Quote Originally Posted by erataille View Post

    The format is just a out file, so it like a txt file.

    There are no delimiters, only blanks
    why don't you upload the text File ? cause it's just a guessing game
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

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