-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

3
  • Hello, did you see this post about csv parsing ? If you already try to parse your csv you should add an example Commented Sep 16, 2018 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/ Commented Sep 16, 2018 at 12:47
  • Possible duplicate of Parsing CSV files in C#, with header Commented Sep 16, 2018 at 16:19

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.