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.
Hi Hossein Tavakoli,
Please find answer for your queries below,
|
Queries |
Solutions |
|
|
How can I set column width same as SfDataGrid column width
|
|
|
|
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,
UG Link: https://help.syncfusion.com/wpf/datagrid/printing#setting-different-row-height
|
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
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]));
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
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;
}
}
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
- 5 Replies
- 2 Participants
-
HT Hossein Tavakoli
- Apr 25, 2022 09:40 AM UTC
- May 9, 2022 05:42 AM UTC