-
May 18th, 2021, 09:23 AM
#1
Thread Starter
New Member
Create a DataTable from a txt file
Hi, I want to create a DataTable in order to display it as DataGridView.
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)
-
May 18th, 2021, 09:54 AM
#2
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
-
May 18th, 2021, 04:23 PM
#3
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?
-
May 18th, 2021, 10:54 PM
#4
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.
-
May 19th, 2021, 04:08 AM
#5
Thread Starter
New Member
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
-
May 19th, 2021, 04:32 AM
#6
Re: Create a DataTable from a txt file
Originally Posted by erataille
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.
Originally Posted by erataille
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.
Originally Posted by erataille
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.
-
May 19th, 2021, 09:22 AM
#7
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
-
May 19th, 2021, 10:08 AM
#8
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)
-
May 19th, 2021, 07:39 PM
#9
Re: Create a DataTable from a txt file
Originally Posted by Delaney
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.
Originally Posted by Delaney
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.
-
May 20th, 2021, 12:02 AM
#10
Re: Create a DataTable from a txt file
Originally Posted by erataille
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|