How to use Header text instead Mapping name on sfDataGrid WPF when export to excel?

Hello

I'm using the control sfDataGrid on WPF. When I export to Excel using Syncfusion.XlsIO the columns name are the Mapping names, but I want to use the Header Text, how can I change it?


3 Replies

VS Vijayarasan Sivanandham Syncfusion Team December 7, 2020 02:24 PM UTC

Hi Daniel Pérez,

Thanks for contacting Syncfusion support.

Based on provided information by default HeaderText will display in exported excel. If HeaderText not definded in column. In this case MappingName should display in Exported file. Please refer the below code snippet for your reference,
 
<syncfusion:SfDataGrid x:Name="sfDataGrid"                                
                               ItemsSource="{Binding Path=Orders}" 
                               AutoGenerateColumns="False">             
           <syncfusion:SfDataGrid.Columns> 
                <syncfusion:GridTextColumn MappingName="OrderID" HeaderText="Customer Order product Numbers"/> 
                <syncfusion:GridTextColumn MappingName="CustomerID" HeaderText="Customer Id for product"/> 
                <syncfusion:GridTextColumn MappingName="CustomerName" HeaderText="Customer Name Displays"/> 
                <syncfusion:GridTextColumn MappingName="Country" HeaderText="Customer Contry"/> 
                <syncfusion:GridTextColumn MappingName="UnitPrice" HeaderText="Price for product" /> 
            </syncfusion:SfDataGrid.Columns> 
</syncfusion:SfDataGrid>   

Sample Link: https://www.syncfusion.com/downloads/support/forum/160365/ze/HeaderTextExportedinExcel2034716714

If we misunderstood your requirement, please provide more information regarding the requirement. This would help us to proceed further.

Regards,
Vijayarasan S
 



DP Daniel Pérez December 7, 2020 11:19 PM UTC

Hi Vijayarasan S.

You are right, using the attribute HeaderText works, but I'm putting the header text on a HeaderTemplate. like this:

  Header Template        
                       
                           
                       
                   


At the beginning, I tryied with this, but it fails:

                    HorizontalHeaderContentAlignment="Left"
                    MappingName="Status" 
                    HeaderText="{Binding Converter={StaticResource UpperCaseConverter}, ConverterParameter={x:Static res:Resource.STATE}}"
                    MaximumWidth="70"
                    MinimumWidth="60"
                    TextAlignment="Left">


As you can see, the text comes from a resource file, and uses an uppercase converter, but I get just the word "State".

Using the Header template, I get the word: "ESTADO", as expected.

Is there another alternative or something I'm doing is wrong?

Thank you very much for your help.



VS Vijayarasan Sivanandham Syncfusion Team December 8, 2020 03:24 PM UTC

Hi Daniel Pérez,

Thanks for the update.

Based on provided information we have tried to apply the header text codebehind from using converter. In this case HeaderText properly exported in Excel. Please refer the below code snippet for your reference,

XAML Code snippet:
 
<syncfusion:SfDataGrid x:Name="sfDataGrid"  
                               EnableDataVirtualization="True" 
                               ItemsSource="{Binding Path=Orders}" 
                               AutoGenerateColumns="False">             
           <syncfusion:SfDataGrid.Columns> 
                <syncfusion:GridTextColumn MappingName="OrderID" HeaderText="{Binding Converter={StaticResource converter}}" /> 
                <syncfusion:GridTextColumn MappingName="CustomerID" HeaderText="Customer Id for product"/> 
                <syncfusion:GridTextColumn MappingName="CustomerName" HeaderText="Customer Name Displays"/> 
                <syncfusion:GridTextColumn MappingName="Country" HeaderText="Customer Contry"/> 
                <syncfusion:GridTextColumn MappingName="UnitPrice" HeaderText="Price for product" /> 
            </syncfusion:SfDataGrid.Columns> 
</syncfusion:SfDataGrid>   

C# Code snippet:

 
public class ColorConverter : IValueConverter 
    { 
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
        {             
                return "Customer Order product Numbers";             
        } 
 
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
        { 
            throw new NotImplementedException(); 
        } 
    } 

Please refer the below screen shot for your reference,

 

Sample Link:
https://www.syncfusion.com/downloads/support/forum/160365/ze/ConverterUsingHeaderText873557567

If we misunderstood your requirement, can you please share us below things?
       
        1. Brief replication procedure/video illustration of the reported issue?
        2.
Code snippet related to XAML and Code behind customization in SfDataGrid

It will be helpful for us to check on it and provide you the solution at the earliest.

Regards,
Vijayarasan S
 


Loader.
Up arrow icon