How to set SfDataGrid column width and AutoHight rows in print page

Hi dears,

I have a SfDataGrid with different column with and column text wrapping.

but when I want to print SfDataGrid, columns have same width and AutoHight rows not supported.

How can I set column width same as SfDataGrid column width and AutoHight rows in print setting.



Untitled.jpg


5 Replies

VS Vijayarasan Sivanandham Syncfusion Team April 26, 2022 07:46 AM UTC

Hi Hossein Tavakoli,

Please find answer for your queries below,

Queries

Solutions

 

How can I set column width same as SfDataGrid column width

 


Your requirement to set different column width while printing in SfDataGrid can be achieved by customize the GridPrintManager class and override the GetColumnWidth. Please refer the below code snippet,

private void OnPrintingClicked(object sender, RoutedEventArgs e)

{

            datagrid.PrintSettings.PrintManagerBase = new CustomPrintManager(this.datagrid);

            datagrid.ShowPrintPreview();        

}

 

 

public class CustomPrintManager : GridPrintManager

{

        SfDataGrid dataGrid;

       

        public CustomPrintManager(SfDataGrid grid)

            : base(grid)

        {

            dataGrid = grid;

        }

 

        protected override double GetColumnWidth(string mappingName)

        {

            //Here you can return the customized width of each column.

            return dataGrid.Columns[mappingName].Width;

        }

}



UG Link: https://help.syncfusion.com/wpf/datagrid/printing#printing-customization

 

 

How can I set AutoHight rows in print setting.

 

 

Your requirement to set different AutoHight rows while printing in SfDataGrid can be achieved by customize the GridPrintManager class and override the GetRowHeight. Please refer the below code snippet,

 

private void OnPrintingClicked(object sender, RoutedEventArgs e)

{

            datagrid.PrintSettings.PrintManagerBase = new CustomPrintManager(this.datagrid);

            datagrid.ShowPrintPreview();        

}

 

 

public class CustomPrintManager : GridPrintManager

{

        SfDataGrid dataGrid;

        GridRowSizingOptions gridrowsizing = new GridRowSizingOptions();

        double Height = double.NaN;

        public CustomPrintManager(SfDataGrid grid)

            : base(grid)

        {

            dataGrid = grid;

        }

 

        protected override double GetRowHeight(object record, int rowindex, RowType rowtype)

        {

            if (record != null && rowtype == RowType.DefaultRow)

            {

                if (this.dataGrid.GridColumnSizer.GetAutoRowHeight(record, gridrowsizing, out Height))

                    if (Height > 24)

                        return Height;

 

            }

            return base.GetRowHeight(record, rowindex, rowtype);

        }

}

 

UG Link: https://help.syncfusion.com/wpf/datagrid/printing#setting-different-row-height

KB Link: https://www.syncfusion.com/kb/7764/how-to-set-row-height-based-on-content-while-printing-wpf-datagrid-sfdatagrid

 

 


Please find the sample in the attachment and let us know if you have any concerns in this.


Regards,

Vijayarasan S


Attachment: SfDataGridDemo_57747bac.zip


HT Hossein Tavakoli April 28, 2022 08:14 PM UTC

Thank this is useful,

I changed the code like this:


var PageMargin= DataGrid.PrintSettings.PrintPageMargin.ToString().Split(",");

DataGrid.PrintSettings.PrintManagerBase = new CustomManagerBase(DataGrid,DataGrid.PrintSettings.PrintPageWidth-double.Parse( PageMargin[0])-double.Parse( PageMargin[2]));


--------


public class CustomManagerBase : GridPrintManager

{

SfDataGrid dataGrid;

double pagePrintWidth;

GridRowSizingOptions gridrowsizing = new GridRowSizingOptions();

double Height = double.NaN;

public CustomManagerBase(SfDataGrid grid,double pageWidth) : base(grid)

{

dataGrid = grid;

pagePrintWidth = pageWidth;

}

protected override double GetRowHeight(object record, int rowindex, RowType rowtype)

{

if (record != null && rowtype == RowType.DefaultRow)

{

if (this.dataGrid.GridColumnSizer.GetAutoRowHeight(record, gridrowsizing, out Height))

if (Height > 24)

return Height + 10;

}

return base.GetRowHeight(record, rowindex, rowtype);

}


protected override double GetColumnWidth(string mappingName)

{

double dataGridSize = dataGrid.Columns.Where(_column => _column.IsHidden == false).Sum(_column => _column.ActualWidth);

double sizePercent = (dataGrid.Columns[mappingName].IsHidden==true)?0 : (dataGrid.Columns[mappingName].ActualWidth / dataGridSize);

double columnSize = pagePrintWidth * sizePercent;

return columnSize;

}

}


this code set print columns width exactly same as SfDataGrid columns width. But I have a problem It works for default PrintSettings. if I change the page width or orientation at runtime it not work.


How can I run below code, when I change the page width or orientation at runtime:

DataGrid.PrintSettings.PrintManagerBase = new CustomManagerBase(DataGrid,DataGrid.PrintSettings.PrintPageWidth-double.Parse( PageMargin[0])-double.Parse( PageMargin[2]));



VS Vijayarasan Sivanandham Syncfusion Team April 29, 2022 02:36 PM UTC

Hi Hossein Tavakoli,

Your requirement to change the column width while changing the page width or orientation property at runtime in SfDataGrid can be achieved by calling the PrintManagerBase customized code in PropertyChanged event of SfDataGrid.PrintSettings.PrintManagerBase. Please refer the below code snippet,

datagrid.PrintSettings.PrintManagerBase.PropertyChanged += OnPropertyChanged;

 

private void OnPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)

{

            var PageMargin = datagrid.PrintSettings.PrintPageMargin.ToString().Split(',');

            datagrid.PrintSettings.PrintManagerBase = new CustomPrintManager(datagrid, datagrid.PrintSettings.PrintPageWidth - double.Parse(PageMargin[0]) - double.Parse(PageMargin[2]));

 

}


Please find the sample in the attachment and let us know if you have any concerns in this.


Regards,

Vijayarasan S


Attachment: SfDataGridDemo_2a44266c.zip


HT Hossein Tavakoli May 8, 2022 02:17 PM UTC

This code worked for me to print SfDataGrid exactly like as SfDataGrid column width.


DataGrid.PrintSettings = new Syncfusion.UI.Xaml.Grid.PrintSettings();
var pageMargin = DataGrid.PrintSettings.PrintPageMargin.ToString().Split(",");

DataGrid.PrintSettings.PrintManagerBase = new CustomManagerBase(DataGrid, DataGrid.PrintSettings.PrintPageWidth - double.Parse(pageMargin[0]) - double.Parse(pageMargin[2]));


DataGrid.PrintSettings.PrintManagerBase.PropertyChanged += (sender, e) =>
{
var pageMargin = DataGrid.PrintSettings.PrintPageMargin.ToString().Split(",");
DataGrid.PrintSettings.PrintManagerBase = new CustomManagerBase(DataGrid, ((CustomManagerBase)sender).PrintPageWidth - double.Parse(pageMargin[0]) - double.Parse(pageMargin[2]));
};
DataGrid.PrintSettings.AllowColumnWidthFitToPrintPage = true;

DataGrid.PrintSettings.AllowRepeatHeaders = true;
DataGrid.PrintSettings.AllowPrintByDrawing = false;

DataGrid.PrintSettings.AllowPrintStyles = false;
DataGrid.ShowPrintPreview();




public class CustomManagerBase : GridPrintManager
{
SfDataGrid dataGrid;
static double pagePrintWidth;
GridRowSizingOptions gridrowsizing = new GridRowSizingOptions();
double Height = double.NaN;
public CustomManagerBase(SfDataGrid grid, double pageWidth) : base(grid)
{
dataGrid = grid;
pagePrintWidth = pageWidth;
}


protected override double GetRowHeight(object record, int rowindex, RowType rowtype)
{
if (record != null && rowtype == RowType.DefaultRow)
{
if (this.dataGrid.GridColumnSizer.GetAutoRowHeight(record, gridrowsizing, out Height))
if (Height > 24)
return Height + 10;
}
return base.GetRowHeight(record, rowindex, rowtype);
}


protected override double GetColumnWidth(string mappingName)
{
double dataGridSize = dataGrid.Columns.Where(_column => _column.IsHidden == false).Sum(_column => _column.ActualWidth);
double sizePercent = (dataGrid.Columns[mappingName].IsHidden == true) ? 0 : (dataGrid.Columns[mappingName].ActualWidth / dataGridSize);
double columnSize = pagePrintWidth * sizePercent;
return columnSize;
}
}


VS Vijayarasan Sivanandham Syncfusion Team May 9, 2022 05:42 AM UTC

Hi Hossein Tavakoli,

We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you😊.

Regards,
Vijayarasan S



Loader.
Up arrow icon