0

I have a program that goes through a CSV file and corrects some errors in the file from the export. (The original program will be fixed, this is a stopgap).

The file is imported into a datagrid, corrected and output to another datagrid for viewing/confirmation before writing the corrected file. To correct the file, it is copied to the clipboard and output using the following:

            correctedFile.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;
            // Select all the cells

            correctedFile.SelectAll();

            DataObject dataObject = correctedFile.GetClipboardContent();

            File.WriteAllText(newFilePath + "\\" + newFileName, dataObject.GetText(TextDataFormat.CommaSeparatedValue));

However, one thing I overlooked is that the original file is a quoted CSV file. Can I easily copy the data to the clipboard with quotes? Or what is the best recourse to do this?

Edit:

"idnum","div","22071","correct","11/13/2021"
"idnum","div","22071","correct","11/13/2021"
"idnum","divXX","22071","corectXX","11/13/2021"
"idnum ","div","22071","correct","11/13/2021"
"idnum","div","22071","correctXX","11/13/2021"

That is the file as it starts. I am using winforms for the visual representation to the user. I load that into a datagridview, clone it into a second datagridview, and any "correctXX" marking that does not have a XX matching marking in the "div" field gets the XX removed. (So line 5, correctXX would be changed to correct).

Once that is done, I use the clipboard copy/write process to write it to a file. Somewhere in there I lose the quotes around each field.

JohnP
  • 402
  • 1
  • 8
  • 25
  • I am not understanding what the grid contains. Does the grid have the quotes in the cells text? Does the original file have the quotes and they get removed when read? It would appear that since you are relying on the “ClipboardCopyMode” and ``File.WriteAllText” … then you may be limiting your options as to what you are wanting to do. Is there some reason you do not simply loop through the grid or its data source and write the cells to the file as you are wanting? Sorry if I am missing something. – JohnG Dec 09 '21 at 21:58
  • @JohnG - See the edit. – JohnP Dec 09 '21 at 22:24
  • How is the code reading the text file into the grid and after it reads the file are the quotes still there in the grid cells? – JohnG Dec 09 '21 at 22:28
  • @JohnG - SQL command to select from file and set a datasource and then bind to that. And no, they are not there. - Oledb connection ("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + importPath + "; Extended Properties = \"Text;HDR=NO;FMT=Delimited(,)\""); – JohnP Dec 09 '21 at 22:30
  • Well… I am pretty sure your code is going to have to add the quotes as you are wanting. I am guessing there may be some third-party CSV Writer that may add the quotes and comma delimiter for you or possibly a LINQ command. As I commented previously, is there some reason you do not simply loop through the grids data source and write the “quoted” strings as you want? – JohnG Dec 09 '21 at 23:06
  • @JohnG - I ended up using the LINQ answer here - https://stackoverflow.com/questions/9943787/exporting-datagridview-to-csv-file - Now I'm trying to right pad the first cell. Everything else is working. Thank you for the assist. – JohnP Dec 10 '21 at 14:41

0 Answers0