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:
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<wpfTool:DateTimePicker Value="{Binding PropertyName}"></wpfTool:DateTimePicker>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<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:
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[
^]