We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

SfDataGrid Printing Header Template with Costum Elements

Greetings,
     I have a Grid that contains a SfDataGrid and a ComboBox, I'm trying to Print the SfDataGrid and in the Header the SelectedItem of the ComboBox knowing that it is bound to a database (the items of the ComboBox change constantly), How can it Print the both in the same Page ?

3 Replies

JG Jai Ganesh S Syncfusion Team March 28, 2017 12:44 AM UTC

Hi Amine, 
You can achieve your requirement for printing the both SfDataGrid and ComboBox in same page and the ComboBox SelectedITem as the header of SfDataGrid by using the PrintPageHeaderTemplate. The header text can be shown by using visual brush and text block. 
Using Visualbrush: 
private void Button_Click(object sender, RoutedEventArgs e) 
{ 
 
    VisualBrush visualbrush = new VisualBrush(); 
    visualbrush.Visual = stackpanel; 
             
    FrameworkElementFactory grid = new FrameworkElementFactory(typeof(Grid)); 
    FrameworkElementFactory borderelement = new FrameworkElementFactory(typeof(Border)); 
    borderelement.SetValue(Border.BackgroundProperty, visualbrush); 
    borderelement.SetValue(Border.HeightProperty, 75.0); 
    borderelement.SetValue(Border.WidthProperty, 1020.0); 
    borderelement.SetValue(Border.HorizontalAlignmentProperty, HorizontalAlignment.Stretch); 
    borderelement.SetValue(Border.VerticalAlignmentProperty, VerticalAlignment.Stretch); 
 
    grid.AppendChild(borderelement); 
 
 
    datatemplate.VisualTree = grid; 
 
    this.datagrid.PrintSettings.AllowRepeatHeaders = false; 
    datagrid.PrintSettings.PrintPageHeaderTemplate = datatemplate; 
    datagrid.PrintSettings.PrintPageHeaderHeight = 55; 
 
    this.datagrid.ShowPrintPreview(); 
} 
 
 
using TextBlock: 
private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    DataTemplate datatemplate = new DataTemplate(); 
             
    TextBlock textblock = new TextBlock(); 
    textblock.Text = combobox.SelectedItem.ToString(); 
 
    FrameworkElementFactory grid = new FrameworkElementFactory(typeof(Grid)); 
            
    FrameworkElementFactory textblockfactory = new FrameworkElementFactory(typeof(TextBlock)); 
 
 
    textblockfactory.SetValue(TextBlock.TextProperty, textblock.Text); 
    textblockfactory.SetValue(TextBlock.BackgroundProperty, Brushes.Gray); 
    grid.AppendChild(textblockfactory); 
 
    datatemplate.VisualTree = grid; 
 
 
    this.datagrid.PrintSettings.AllowRepeatHeaders = false; 
    datagrid.PrintSettings.PrintPageHeaderTemplate = datatemplate; 
    datagrid.PrintSettings.PrintPageHeaderHeight = 25; 
 
    this.datagrid.ShowPrintPreview(); 
} 
 
Sample: 
 
Reference: 
Regards, 
Jai Ganesh S 



AL Amine Laïeb March 29, 2017 10:20 AM UTC

Thank you very much works perfectly


MK Muthukumar Kalyanasundaram Syncfusion Team March 30, 2017 05:16 PM UTC

Hi Amine, 
 
You are welcome. Please let us know if you need any other assistance. 
 
Regards, 
Muthukumar K 


Loader.
Live Chat Icon For mobile
Up arrow icon