Force SfDataGrid to Print using dispayed/custom column widths, not the default equally sized columns

SfDataGrid allows GridTextColumns to have individual widths set, some narrow, some wide etc. However, when using ShowPrintPreview on such a customised grid the column widths are all equal resulting in some column data having too much trailing white space, while the data in other columns is cut off and only partially visible.

Is there a way to force the ShowPrintPreview feature to use the current custom set widths of the GridTextColumns?


Thanks,

Adrian


4 Replies

AD Adrian February 5, 2018 10:59 AM UTC

Solved...

Override GridPrintManager with a CustomPrintManager and override method GetColumnWidth:

protected override double GetColumnWidth(string mappingName)
{
      var column = dataGrid.Columns.FirstOrDefault(a => a.MappingName == mappingName);
      return column.Width;
}


GT Gnanasownthari Thirugnanam Syncfusion Team February 5, 2018 11:10 AM UTC

Hi Adrian 

Yes, your requirement can be achieved by customize the GridPrintManager class and override the GetColumnWidth as you specified. 
 
datagrid.PrintSettings.PrintManagerBase = new CustomPrintManager(this.datagrid); 
 
public class CustomPrintManager : GridPrintManager 
{ 
    public CustomPrintManager(SfDataGrid grid) 
        : base(grid) 
    { 
    } 
 
    protected override double GetColumnWidth(string mappingName) 
    {            
        //Here you can return the customized width of each column. 
        return this.dataGrid.Columns[mappingName].Width; 
 
    } 
} 

We have prepared the sample based on your requirement, you can download it from below mentioned location. 


You can also calculate the width based on cell content, header and Auto width like below code example. 

datagrid.PrintSettings.PrintManagerBase = new CustomPrintManager(this.datagrid); 
 
public class CustomPrintManager : GridPrintManager 
{ 
    public CustomPrintManager(SfDataGrid grid) 
        : base(grid) 
    { 
    } 
 
    protected override double GetColumnWidth(string mappingName) 
    { 
        if (mappingName == "OrderID") 
        { 
            //Here you can calculate the Auto Width while printing 
            GridColumn gridColumn = this.dataGrid.Columns[mappingName]; 
            MethodInfo methodInfo = this.dataGrid.GridColumnSizer.GetType().GetMethod("CalculateAutoFitWidth", BindingFlags.NonPublic | BindingFlags.Instance); 
            return (double)methodInfo.Invoke(dataGrid.GridColumnSizer, new object[] { gridColumn, true }); 
        } 
        else if (mappingName == "CustomerID") 
        { 
            //Here you can calculate the SizeToCell Width while printing 
            GridColumn gridColumn = this.dataGrid.Columns[mappingName]; 
            MethodInfo methodInfo = this.dataGrid.GridColumnSizer.GetType().GetMethod("CalculateCellWidth", BindingFlags.NonPublic | BindingFlags.Instance); 
            return (double)methodInfo.Invoke(dataGrid.GridColumnSizer, new object[] { gridColumn, true }); 
        } 
        else if (mappingName == "CustomerName") 
        { 
            //Here you can calculate the SizetoHeader Width while printing 
            GridColumn gridColumn = this.dataGrid.Columns[mappingName]; 
            MethodInfo methodInfo = this.dataGrid.GridColumnSizer.GetType().GetMethod("CalculateHeaderWidth", BindingFlags.NonPublic | BindingFlags.Instance); 
            return (double)methodInfo.Invoke(dataGrid.GridColumnSizer, new object[] { gridColumn, true }); 
        } 
        else if (mappingName == "Country") 
        { 
            //Here you can return the customized width of each column. 
            return this.dataGrid.Columns[mappingName].Width; 
        } 
        else 
            return base.GetColumnWidth(mappingName); 
        
    } 
} 

Please find the sample for the same from below mentioned location. 

Please let us know If you need further assistance on this, we will happy to assist you. 

Regards, 
Gnanasownthari T. 
 



AD Adrian February 5, 2018 10:27 PM UTC

Thanks for the details, that's great.


GT Gnanasownthari Thirugnanam Syncfusion Team February 6, 2018 05:13 AM UTC

Hi Adrian, 

Thank you for your update, 

Please let us know if you need further assistance on this. 

Regards, 
Gnanasownthari T. 


Loader.
Up arrow icon