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

Printing grid with custom foreground/text color and no/adjusted borders

Hi,

I first want to say that you guys have phenomenal support! 

So, I want to print a datagrid and it works pretty easy, like the docs say. But I want to tweak how it looks. My datagrid does not have any borders on the screen, except a few underlines (border bottom), and I want to have this on the print as well. Is that possible? I read the docs and the only thing I found was setting the background... Foreground color would be nice too, but that's not that important.

Looking forward to a reply :-)

Kind regards,
Erik

7 Replies

SP Shobika Palani Syncfusion Team June 19, 2019 12:08 PM UTC

Hi Erik, 

Thank you for contacting Syncfusion Support. 

We have analyzed your query to print the grid cell border and foreground same as in view. And you can achieve this by overriding AddDataRowToPanel method and GetColumnElement method in CustomPrintManager class. Also you need to set PrintSettings.AllowPrintByDrawing as false and PrintSettings.AllowPrintStyles as true. Please refer to the below code example 

dataGrid.PrintSettings.PrintManagerBase = new CustomPrintManagerBase(dataGrid); 
dataGrid.PrintSettings.AllowPrintByDrawing = false; 
dataGrid.PrintSettings.AllowPrintStyles = true; 
dataGrid.ShowPrintPreview(); 
 
 
 
public class CustomPrintManagerBase : GridPrintManager 
    { 
        public CustomPrintManagerBase(SfDataGrid dataGrid) : base(dataGrid) 
        { 
 
        } 
 
        protected override object GetColumnElement(object record, string mappingName) 
        { 
            var columnElement = base.GetColumnElement(record, mappingName) as TextBlock; 
            if (columnElement != null) 
                columnElement.Foreground = new SolidColorBrush(Colors.Blue); 
            return columnElement; 
        } 
        protected override void AddDataRowToPanel(PrintPagePanel panel, RowInfo rowInfo) 
        { 
            base.AddDataRowToPanel(panel, rowInfo); 
            var topThickNess = rowInfo.NeedTopBorder ? 1 : 0; 
            var bottomThickness = rowInfo.NeedBottomBorder ? 1 : 0; 
            var record = (rowInfo.Record is RecordEntry) ? (rowInfo.Record as RecordEntry).Data : rowInfo.Record; 
            var i = 0; 
            foreach (var cellInfo in rowInfo.CellsInfo) 
            { 
                var cell = GetPrintGridCell(record, cellInfo.ColumnName); 
                cell.Width = cellInfo.CellRect.Width; 
                cell.Height = cellInfo.CellRect.Height; 
                cell.BorderThickness = i == 0 ? 
                    new Thickness(1, 0, 0, 1) : i == dataGrid.Columns.Count - 1 ? new Thickness(0, 0, 1, 1) : new Thickness(0, 0, 0, 1); 
 
                var content = GetColumnElement(record, cellInfo.ColumnName); 
                if (content is DataTemplate) 
                { 
                    cell.ContentTemplate = content as DataTemplate; 
                    SetDataTemplateContentToPrintGridCell(cell, cellInfo, record); 
                } 
                else 
                    cell.Content = content; 
                cellInfo.Element = cell; 
                panel.Children.Add(cell); 
                i++; 
            } 
        } 
 
        internal void SetDataTemplateContentToPrintGridCell(ContentControl cell, CellInfo cellInfo, object record) 
        { 
            cell.Content = record; 
        } 
    } 

Also please find sample for the same from the below link 
Sample Link: 

If the above solution does not meet your requirement, please revert us with more details like image/video illustration on your requirement. It will helps us to investigate further and provide earlier prompt solution. 

Regards, 
Shobika. 



ER Erik June 19, 2019 12:17 PM UTC

Many many thanks! I am going to apply this code later this day, but I'm very hopefull :-)

Have a great day and many thanks for the extensive and specific reply!


JP Jagadeesan Pichaimuthu Syncfusion Team June 19, 2019 12:21 PM UTC

Hi Erik, 
 
Thanks for the update. 
 
We will wait to hear from you. 
 
Regards, 
Jagadeesan 



ER Erik June 20, 2019 10:49 PM UTC

Great, it works!

Got some other things sorted out as well, so that's nice :-)

One other question! I've got text that is longer than the cell, so I tried to set the TextWrapping like this but that doesn't work...

protected override object GetColumnElement(object record, string mappingName)
{
var columnElement = base.GetColumnElement(record, mappingName) as TextBlock;

columnElement.TextWrapping = TextWrapping.NoWrap;
if (columnElement != null)
columnElement.Foreground = new SolidColorBrush(Colors.Blue);
return columnElement;
}

The result is this:


How can I:
1. Set NoWrap on
2. Make the middle column size automatically while having the others set to a fixed width. (so NoWrap is hopefully not needed).

This is my sfDataGrid in the app with the  ColumnSizer="AutoLastColumnFill" at 2nd column:


Many thanks!


SP Shobika Palani Syncfusion Team June 21, 2019 04:40 AM UTC

Hi Erik, 

Thanks for the update. 

We have analyzed your query to fit the column width as in view while printing. And this requirement can be achieved by overriding GetColumnWidth method in CustomPrintManagerBase class. Please refer to the below code snippet 

protected override double GetColumnWidth(string mappingName) 
        { 
            if (mappingName == "EmployeeArea") 
                return this.dataGrid.Columns[mappingName].ActualWidth; 
            return base.GetColumnWidth(mappingName); 
        } 

Please find sample for the same from the link below 

Sample Link: 

Please let us know, if you require further assistance on this. 

Regards, 
Shobika. 



ER Erik June 21, 2019 12:16 PM UTC

Thanks Shobika, I'll try! I have good hopes again :-)

Is there by the way any possibility to have the TextWrapping working as well? 


SP Shobika Palani Syncfusion Team June 24, 2019 08:43 AM UTC

Hi Erik, 

We have analyzed your query to set text wrapping while printing. And your requirement can be achieved by setting ColumnElement.TextWrapping as Wrap as like below code snippet 

protected override object GetColumnElement(object record, string mappingName) 
        { 
            var columnElement = base.GetColumnElement(record, mappingName) as TextBlock; 
            if (columnElement != null) 
            { 
                columnElement.Foreground = new SolidColorBrush(Colors.Blue); 
                columnElement.TextWrapping = TextWrapping.Wrap; 
            } 
            return columnElement; 
        } 

Also please refer to the below KB article to know more details on printing with row height based on content from the link below 

KB link: 

Please let us know, if you require further assistance on this. 

Regards, 
Shobika. 


Loader.
Live Chat Icon For mobile
Up arrow icon