-3

I am looking to import a csv file in a windows form, datagridvew1 and then parse/update some of the columns into datagridview2, and finally write the output as csv I am struggling to convert the required fields from datagridview1 into datagridview2 Any suggestions welcome. Thanks

Ryan80
  • 31
  • 1
  • 3
  • Hello, did you see this [post about csv parsing](https://stackoverflow.com/questions/2081418/parsing-csv-files-in-c-with-header) ? If you already try to parse your csv you should [add an example](https://stackoverflow.com/help/how-to-ask) – AnderCover Sep 16 '18 at 11:35
  • Parsing DGV columns makes no sense. There are gobs of libraries out there to read and parse CSVs and create lists and collections of class objects/ – Ňɏssa Pøngjǣrdenlarp Sep 16 '18 at 12:47
  • Possible duplicate of [Parsing CSV files in C#, with header](https://stackoverflow.com/questions/2081418/parsing-csv-files-in-c-with-header) – Ganesh Kathiresan Sep 16 '18 at 16:19

1 Answers1

0

There's a million ways to do this but my suggestion would be assuming you are writing your code for windows use the microsoft text driver to load the data directly from the text file into a dataset (roughly as shown in: CSV upload in .NET using ODBC including the answer's fix for the bug in the posters code). Then clone and dump out the data into the second dataset/csv file as so: https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/dataset-datatable-dataview/copying-dataset-contents .

Lastly: I would suggest not blindly looping though the datarows and outputting row[i] + "," etc like a see a lot of posts on similar questions suggesting. The problem is escaping fields that contain commas themselves properly etc. Better to use a library, one I've used in the past and found nice is CsvParser you can get that via nuget. It's better to use libaries anyways because when the next complication comes up like utf-8 encoded text often you just have to pass in an additional parameter rather than learning and properly covering all the edge cases of how to do the encoding yourself.

Mike
  • 418
  • 3
  • 10