private void btnPrint_Click(object sender, RoutedEventArgs e)
{
//print the data displayed in the DataGrid using SfDataGrid.Print method.
//sfDataGrid.Print();
//Customize the Print settings in SfDataGrid
sfDataGrid.PrintSettings.PrintManagerBase = new CustomManagerBase(this.sfDataGrid);
sfDataGrid.PrintSettings.AllowPrintByDrawing = true;
//provides option to display print preview of SfDataGrid rows
sfDataGrid.ShowPrintPreview();
}
public class CustomManagerBase : GridPrintManager
{
public SfDataGrid DataGrid;
internal IList source;
public CustomManagerBase(SfDataGrid grid) : base(grid)
{
DataGrid = grid;
}
private int GetUnBoundRowCount(UnBoundRowsPosition position, bool isbelowsummary)
{
int count = 0;
if (position == UnBoundRowsPosition.Top && !isbelowsummary)
count = dataGrid.GetUnBoundRowsCount(UnBoundRowsPosition.Top, false);
else if (position == UnBoundRowsPosition.Top && isbelowsummary)
count = dataGrid.GetUnBoundRowsCount(UnBoundRowsPosition.Top, true);
else if (position == UnBoundRowsPosition.Bottom && isbelowsummary)
count = dataGrid.GetUnBoundRowsCount(UnBoundRowsPosition.Bottom, true);
else if (position == UnBoundRowsPosition.Bottom && !isbelowsummary)
count = dataGrid.GetUnBoundRowsCount(UnBoundRowsPosition.Bottom, false);
return count;
}
protected override IList GetSourceListForPrinting()
{
var source = (View as PagedCollectionView).GetInternalList();
List<object> OrderedSource = new List<object>();
if (GetUnBoundRowCount(UnBoundRowsPosition.Top, false) > 0)
{
foreach (var row in dataGrid.UnBoundRows.Where(r => r.Position == UnBoundRowsPosition.Top && !r.ShowBelowSummary))
OrderedSource.Add(row);
}
if (GetTableSummaryList(TableSummaryRowPosition.Top).Count > 0)
{
foreach (var item in GetTableSummaryList(TableSummaryRowPosition.Top))
OrderedSource.Add(item);
}
if (GetUnBoundRowCount(UnBoundRowsPosition.Top, true) > 0)
{
foreach (var row in dataGrid.UnBoundRows.Where(r => r.Position == UnBoundRowsPosition.Top && r.ShowBelowSummary))
OrderedSource.Add(row);
}
foreach (var item in source)
OrderedSource.Add(item);
if (GetUnBoundRowCount(UnBoundRowsPosition.Bottom, false) > 0)
{
foreach (var row in dataGrid.UnBoundRows.Where(r => r.Position == UnBoundRowsPosition.Bottom && !r.ShowBelowSummary))
OrderedSource.Add(row);
}
if (GetTableSummaryList(TableSummaryRowPosition.Bottom).Count > 0)
{
foreach (var item in GetTableSummaryList(TableSummaryRowPosition.Bottom))
OrderedSource.Add(item);
}
if (GetUnBoundRowCount(UnBoundRowsPosition.Bottom, true) > 0)
{
foreach (var row in dataGrid.UnBoundRows.Where(r => r.Position == UnBoundRowsPosition.Bottom && r.ShowBelowSummary))
OrderedSource.Add(row);
}
return OrderedSource;
}
} |