I am interested in the fastest, easiest way to import a text file into the grid. These text files contain an indeterminate number of rows and columns. Data in each row is delimited, in that if there are let's say 10 columns, you're going to have 10 values delimited by 9 delimiters.The delimiter character is also dynamic, passed into the app as an argument - so, it can be anything from a tilde to a comma to a tab. I want to iterate through the text (which I've put into a list via filestream, though if there's a better way...), then parse each field (ie. traverse the delimiters), and throw it into a cell in a grid.
so, say a csv file like the one below would result in a grid with 3 rows and 4 columns:
aaa,123,554,221
bbb,432,555,23
jjj,5455,333,1
thanks
Oh, Ideally the first row of the data would contain the column names, so, also, how to name each column?
Is the best solution for all of this to create a data table and then use it as the grid's data source?
Thanks.
Yep, that's the best way!
Never mind!
Hello Samuel,
Yes, you will have to load the CSV file into a structure, like a DataTable and then bind the grid’s DataSource to it. So, you can use StreamReader to read all the lines of the file and split the line’s data by all the possible delimiters.
You may find helpful the following forum thread as it provides a similar sample code.
Unfortunately, I'm building a sort of jack of all trades app, that must import data from CSVs, tab-delimited, tilde-delimited, any type of valid delimiter. But I got it working fine, at least that part.
Hi Samuel,
Thank you for the update on this matter. Please let me know if you have any further questions.