I've implemented printing on my grid in a way very similar to one described here: http://www.telerik.com/community/forums/silverlight/gridview/print-grid-in-color.aspx.
My grid's columns are built in code. When it is populated with data, I have 20 groups and about 1000 records in all of them combined. Exporting works fine, but printing takes forewer. Printing works better on smaller data sets. Is there a way to speed up the printing?
Essentially, there are three methods involved in the print event: btnPrint_Click, FormatGrid (where I generate a grid, called _grid, based on the one displayed, caleld rgvMainGrid), and doc_PrintPage. Here is the code:
double offsetY;
double totalHeight;
Canvas canvas;
RadGridView _grid;
private void btnPrint_Click(object sender, RoutedEventArgs e)
{
try
{
PrintDocument doc = new PrintDocument();
FormatGrid("print");
canvas = new Canvas();
if (_grid.Items.Groups.Count > 0)
{
for (int i = 0; i < _grid.Items.Groups.Count; i++ )
{
this._grid.ExpandGroup(this._grid.Items.Groups[i] as IGroup); //expanding the groups before printing the list
}
}
canvas.Children.Add(_grid);
doc.PrintPage += this.doc_PrintPage;
doc.Print("Test Print");
}
catch (Exception ex)
{
TrackException(ex, "method: btnPrint_Click");
}
}
void FormatGrid(string action)
{
try
{
offsetY = 0d;
totalHeight = 0d;
int counter = 0;
_grid = new RadGridView();
_grid.DataContext = rgvMainGrid.DataContext;
_grid.ItemsSource = rgvMainGrid.ItemsSource;
_grid.RowIndicatorVisibility = Visibility.Collapsed;
_grid.ShowGroupPanel = false;
_grid.CanUserFreezeColumns = false;
_grid.IsFilteringAllowed = false;
_grid.AutoExpandGroups = true;
_grid.AutoGenerateColumns = false;
foreach (GridViewColumn column in rgvMainGrid.Columns.OfType<GridViewColumn>())
{
if(counter > 1) //Need to exclude first two columns
{
GridViewColumn newColumn = (GridViewColumn)Activator.CreateInstance(column.GetType());
newColumn.CopyPropertiesFrom(column);
_grid.Columns.Add(newColumn);
}
counter++;
}
StyleManager.SetTheme(_grid, StyleManager.GetTheme(rgvMainGrid));
_grid.SortDescriptors.AddRange(rgvMainGrid.SortDescriptors);
_grid.GroupDescriptors.AddRange(rgvMainGrid.GroupDescriptors);
_grid.FilterDescriptors.AddRange(rgvMainGrid.FilterDescriptors);
ScrollViewer.SetHorizontalScrollBarVisibility(_grid, ScrollBarVisibility.Hidden);
ScrollViewer.SetVerticalScrollBarVisibility(_grid, ScrollBarVisibility.Hidden);
if (!string.IsNullOrEmpty(action) && action.ToLower() != "export_to_excel")
{
_grid.ElementExporting += new EventHandler<GridViewElementExportingEventArgs>(rgvMainGrid_ElementExporting);
_grid.ElementExported += new EventHandler<GridViewElementExportedEventArgs>(rgvMainGrid_ElementExported);
}
}
catch (Exception ex)
{
TrackException(ex, "method: FormatGrid(string action)");
}
}
void doc_PrintPage(object sender, PrintPageEventArgs e)
{
try
{
e.PageVisual = canvas;
if (totalHeight == 0)
{
totalHeight = _grid.DesiredSize.Height;
}
Canvas.SetTop(_grid, -offsetY);
//PrintableArea
offsetY += e.PrintableArea.Height;
e.HasMorePages = offsetY <= totalHeight;
}
catch (Exception ex)
{
TrackException(ex, "method: doc_PrintPage");
}
}
Any help would be greatly appreciated!
Thank you!
-VK.
