Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to export my DataGrid (name: myDataGrid) to .csv - but have ran into some issues. <Textcolumn> export as intended, but my <Datatemplate> <ComboBox> and <wpftool:datetimepicker> do not. Their cells end up empty.

Here's the XAML for the two columns:

XML
<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
             <wpfTool:DateTimePicker Value="{Binding PropertyName}"></wpfTool:DateTimePicker>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>


XML
<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox SelectedValuePath="{Binding ExampleName, UpdateSourceTrigger=PropertyChanged}">
                <ComboBoxItem Content="25" Tag="0" />
                <ComboBoxItem Content="15" Tag="1" />
                <ComboBoxItem Content="12" Tag="2" />
                <ComboBoxItem Content="6" Tag="3" />
            </ComboBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>


And my "ExportToCSV"-method:

C#
myDataGrid.SelectAllCells();
myDataGrid.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
ApplicationCommands.Copy.Execute(null, myDataGrid);
string result1 = (string)Clipboard.GetData(DataFormats.CommaSeparatedValue);
string result2 = (string)Clipboard.GetData(DataFormats.Text);
myDataGrid.UnselectAllCells();
System.IO.StreamWriter file1 = new("My:\File\Path\test.xls");
file1.WriteLine(result2.Replace(',', ' '));
file1.Close();


What I have tried:

I have been trying to set up the ClipboardContentBinding event, but could not get it working. I am using MVVM, but would be fine with a code behind solution as well.

I was not able to implement the solution provided in the accepted answer at Bind ClipboardContentBinding to DisplayMemberPath[^]
Posted
Updated 23-Aug-21 2:11am
v2
Comments
[no name] 23-Aug-21 12:04pm    
The SO answer "almost worked" and had 2 votes; indicating it's not that popular a technique. Sounds like a kludge for an ill defined function.
Flidrip 23-Aug-21 15:09pm    
You are probably right. I tried implementing this solution: https://blog.magnusmontin.net/2013/09/29/export-data-from-a-datagrid/
but it's way too complex for me. I spent hours implementing it, but the problem still remains. Do you have any tips in how to go about this? Maybe you know of a good video, or blog? Nothing I try seem to be working.
[no name] 25-Aug-21 12:30pm    
The grid is a "visual". It's "data driven". You should be focusing on the backing data store; which is independent of any representation; and more understandable.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900