Summarize the problem: In the WPF grid, data is displayed using value converters (IValueConverter). When exporting with TelerikDataGrid.Export(), Telerik uses the raw bound values instead of the converted display values. This causes a mismatch: what you see in the grid is different from what appears in the exported Excel file.
Tried Method:
Identified the root cause of the issue, the grid shows converted values (via IValueConverter).
But after export uses raw bound values, will exported. I need to export the Grid displayed value into the Excel.
M_DataGrid_ElementExporting
private void M_DataGrid_ElementExporting(object sender, Telerik.Windows.Controls.GridViewElementExportingEventArgs e)
{
if (e.Element == Telerik.Windows.Controls.ExportElement.Cell)
{
var cellExportingArgs = e as GridViewCellExportingEventArgs;
if (e.Context is Telerik.Windows.Controls.GridViewDataColumn dataColumn)
{
var col = e.Context as Telerik.Windows.Controls.GridViewDataColumn;
if (col != null && col.UniqueName == "MomentOfInertia")
{
var binding = dataColumn.DataMemberBinding;
var propertyValue = TelerikDataVis_Doc.GetPropertyValue(e.DataContext, binding.Path.Path);
if (binding != null)
{
object rawValue = e.Value;
if (binding.Converter != null)
{
rawValue = binding.Converter.Convert(
propertyValue,
typeof(string),
binding.ConverterParameter,
System.Globalization.CultureInfo.CurrentCulture);
}
if (!string.IsNullOrEmpty(binding.StringFormat))
{
e.Value = string.Format(binding.StringFormat, rawValue);
}
else
{
e.Value = rawValue?.ToString();
}
}
}
}
}
}
Tried Method:
Identified the root cause of the issue, the grid shows converted values (via IValueConverter).
But after export uses raw bound values, will exported. I need to export the Grid displayed value into the Excel.
M_DataGrid_ElementExporting
private void M_DataGrid_ElementExporting(object sender, Telerik.Windows.Controls.GridViewElementExportingEventArgs e)
{
if (e.Element == Telerik.Windows.Controls.ExportElement.Cell)
{
var cellExportingArgs = e as GridViewCellExportingEventArgs;
if (e.Context is Telerik.Windows.Controls.GridViewDataColumn dataColumn)
{
var col = e.Context as Telerik.Windows.Controls.GridViewDataColumn;
if (col != null && col.UniqueName == "MomentOfInertia")
{
var binding = dataColumn.DataMemberBinding;
var propertyValue = TelerikDataVis_Doc.GetPropertyValue(e.DataContext, binding.Path.Path);
if (binding != null)
{
object rawValue = e.Value;
if (binding.Converter != null)
{
rawValue = binding.Converter.Convert(
propertyValue,
typeof(string),
binding.ConverterParameter,
System.Globalization.CultureInfo.CurrentCulture);
}
if (!string.IsNullOrEmpty(binding.StringFormat))
{
e.Value = string.Format(binding.StringFormat, rawValue);
}
else
{
e.Value = rawValue?.ToString();
}
}
}
}
}
}