0
votes

I have a wpf datagrid which is bound to a MVVM collection class. And I have some properties of MVVM class which is bound to datagrid but not every.

I need to export data in Excel CSV file from the datagrid. But there are some properties of the MVVM class which are not bound to datagrid but need to be shown in Excel file. So I need to create custom columns (columns from datagrid+some extra).

Here is the code I am using currently to create the Excel CSV file:

importedDG.SelectAllCells(); 
importedDG.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
ApplicationCommands.Copy.Execute(null, importedDG);
importedDG.UnselectAllCells();
string path1 = "C:\\test.csv"; 
string result1 = (string)Clipboard.GetData(DataFormats.CommaSeparatedValue); 
Clipboard.Clear(); 
System.IO.StreamWriter file1 = new System.IO.StreamWriter(path1);
file1.WriteLine(result1);file1.Close(); 

This creates the file with the exact columns from the DG but not extra which I want. How can I add them?

1
Which part are you struggling with? - Kent Boogaart
I can create a excel file with the same datagrid columns but how to add extra columns which are not there in datagrid. - alice7
How are you creating the excel file currently? - Kent Boogaart
importedDG.SelectAllCells(); importedDG.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader; ApplicationCommands.Copy.Execute(null, importedDG); importedDG.UnselectAllCells(); string path1 = "C:\\test.csv"; string result1 = (string)Clipboard.GetData(DataFormats.CommaSeparatedValue); Clipboard.Clear(); System.IO.StreamWriter file1 = new System.IO.StreamWriter(path1); file1.WriteLine(result1);file1.Close(); THis creates the file with the exact columns from the DG but not extra which I want. - alice7

1 Answers

1
votes

I'm guessing you're using a method on the grid control to produce the CSV? You've not stated as much in your question.

If so, I would move this responsibility to your view model. You can use a CSV library such as KBCsv to perform the task of writing your data.