Using a data grid in C# / WPF is a pleasure…unless you want to export the data in the grid to a CSV (comma-separated) file (and you don’t know this trick)! What happens is you likely Google something like ‘wpf datagrid export to csv’ and Google provides you with a bunch of results, all articles telling you how to parse through the data-grid, extracting and writing the data from the grid one row, one cell at a time. Now if that’s all you see, it actually starts looks pretty complicated to get the data into a CSV file! Luckily, you are here. 🙂
There is what could reasonably be called a really great shortcut to actually exporting the data out of the WPF data-grid, that doesn’t require looping through, or otherwise manipulating the data-grid in any way.
Copying the data:
The WPF data-grid has the SelectAllCells and UnselectAllCells methods. These methods together with the ApplicationCommands type gives us a way to select all the data, and copy it to the Windows Clipboard. This is just as you would have done if you had selected all the cells of the datagrid with your mouse, and pressed ctrl-c on your keyboard:
datagrid_Results.SelectAllCells(); datagrid_Results.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader; ApplicationCommands.Copy.Execute(null, datagrid_Results); datagrid_Results.UnselectAllCells();
Now the data is copied to the clipboard (notice how this means this method won’t be that great for data files that have gigabytes of data 😉 ) – and that’s it! The scary part is over! You have exported the data from the grid!!! Ok, well, you’ve still only exported it to the Windows Clipboard, so…
Writing the data:
Now you simply need to get the data from the Clipboard, and write it out to a file and you’re done!
string result = (string)System.Windows.Clipboard.GetData(System.Windows.DataFormats.CommaSeparatedValue); File.AppendAllText(fullpath), result, UnicodeEncoding.UTF8);
Check the save location, you should now have your grid data, just as it was in the grid, as a CSV file! Easy.
Works like a Charm. Tks
I’ve positioned the code at WPF C# button.
It exports to csv,
but it also crashes the GUI.
Any recommendations ?
Hi, is the copyclipboard method need something condition? when i try that method, it create the csv file but with null value,
why the content is null?
Does this actually put the contents of a datagrid into a csv file? At the moment it is just creating a plain file with no records from the datagrid. Cheers!
Not with this method I am afraid, using the “GetData” of the Clipboard limits your data format possibilities. The only thing I can think of as a way of getting the commas to be semi-colon instead, would be to do a strict find/replace after the data is already in CSV format. That you’d have to be real careful with though, because you would have to consider any commas that might be in the actual data you are storing, rather than just the commas produced by the GetData method using “CommaSeparatedValue”.
It is possible to use a semi-colon (;) instead of the comma ? How can I do that ?
Doh! I was doing something waaaaayyy more complicated than that, looping through the rows of the grid, even then, was having trouble getting the changes user made to cells 🙁 This seems to work great for my purposes, thanks!