This is a migrated thread and some comments may be shown as answers.

Print all columns of the grid

2 Answers 109 Views
GridView
This is a migrated thread and some comments may be shown as answers.
frdotnet
Top achievements
Rank 1
frdotnet asked on 22 Jul 2010, 10:41 PM

HI,

I am fairly new to WPF and I am trying to print preview a grid having more than 10 columns . All the columns do not fit on a page for printing. So I would like to continue printing rest of the columns on page 2 . Or if anyother approach , so that the user can see the data for all the columns of the grid.. Here is the sample code. Please let me know.

public static void PrintPreview(Telerik.Windows.Controls.RadGridView grid)
       {
           if (grid != null)
           {
               var dialog = new PrintDialog();
               var capabilities = dialog.PrintQueue.GetPrintCapabilities(dialog.PrintTicket);
               var pageSize = new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight);
               var extentSize = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
               FixedDocument fixedDoc = (PaginateGridInToFixedDocument(ToPrintFriendlyGrid(grid, extentSize), dialog));
               BusinessObjectGridPrintPreviewView previewWindow = new BusinessObjectGridPrintPreviewView();
               previewWindow.RadGridView = grid;
               previewWindow.Document = fixedDoc;
               previewWindow.ShowDialog();
           }
       }
       public static FixedDocument PaginateGridInToFixedDocument(GridViewDataControl element, PrintDialog dialog)
       {
           //var capabilities = dialog.PrintQueue.GetPrintCapabilities(dialog.PrintTicket);
           //var pageSize = new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight);
           //var extentSize = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
           var capabilities = dialog.PrintQueue.GetPrintCapabilities(dialog.PrintTicket);
           Size pageSize = new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight);
           Size extentSize = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
           var fixedDocument = new FixedDocument();
           var totalHeight = 0.0;
           var totalWidth = 0.0;
           element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
           element.Arrange(new Rect(new Point(0, 0), element.DesiredSize));
           totalHeight = element.DesiredSize.Height;
           //totalWidth = element.DesiredSize.Width;
           List<double> widthBreakList = new List<double>();
           double widthBreakTotal = 0;
           if (element.GroupDescriptors.Count > 0)
           {
               widthBreakTotal = widthBreakTotal + (25 * element.GroupDescriptors.Count);
           }
           double lastBreakLocation = 0;
           widthBreakList.Add(0);
           foreach (Telerik.Windows.Controls.GridViewColumn column in element.Columns)
           {
               if (widthBreakTotal + column.ActualWidth >= (lastBreakLocation + extentSize.Width))
               {
                   widthBreakList.Add(widthBreakTotal);
                   lastBreakLocation = widthBreakTotal;
               }
               else
               {
                   widthBreakTotal += column.ActualWidth;
               }
           }
           totalWidth = widthBreakTotal;
           widthBreakList.Add(totalWidth);
           var yOffset = 0d;
           var xOffset = 0d;
           var xOffsetNext = 0d;
           for (int i = 0; i < (widthBreakList.Count - 1); i++)
           {
               xOffset = widthBreakList[i];
               xOffsetNext = widthBreakList[i + 1];
               yOffset = 0d;
               while (yOffset < totalHeight)
               {
                   var brush = new VisualBrush(element)
                   {
                       Stretch = Stretch.None,
                       AlignmentX = AlignmentX.Left,
                       AlignmentY = AlignmentY.Top,
                       ViewboxUnits = BrushMappingMode.Absolute,
                       TileMode = TileMode.None,
                       Viewbox = new Rect(xOffset, yOffset, xOffsetNext, (extentSize.Height - (extentSize.Height % element.RowHeight)))
                   };
                   var pageContent = new PageContent();
                   var page = new FixedPage();
                   ((IAddChild)pageContent).AddChild(page);
                   fixedDocument.Pages.Add(pageContent);
                   page.Width = pageSize.Width;
                   page.Height = pageSize.Height;
                   var canvas = new Canvas();
                   FixedPage.SetLeft(canvas, capabilities.PageImageableArea.OriginWidth);
                   FixedPage.SetTop(canvas, capabilities.PageImageableArea.OriginHeight);
                   canvas.Width = xOffsetNext - xOffset;
                   canvas.Height = (extentSize.Height - (extentSize.Height % element.RowHeight));
                   canvas.Background = brush;
                   page.Children.Add(canvas);
                   yOffset += extentSize.Height - (extentSize.Height % element.RowHeight);
               }
           }
           return fixedDocument;
       }
       /// <summary>
       /// Convert(Re-create) RadGridView into a print-friendly format
       /// </summary>
       /// <param name="source"></param>
       /// <returns></returns>
       public static GridViewDataControl ToPrintFriendlyGrid(GridViewDataControl source, Size extentSize)
       {
           var grid = new RadGridView()
           {
               ItemsSource = source.ItemsSource,
               RowIndicatorVisibility = Visibility.Collapsed,
               ShowGroupPanel = false,
               CanUserFreezeColumns = false,
               IsFilteringAllowed = false,
               AutoExpandGroups = true,
               AutoGenerateColumns = false,
               ColumnWidth = GridViewLength.SizeToHeader
           };
           grid.ShowColumnFooters = source.ShowColumnFooters;
           grid.ShowGroupFooters = source.ShowGroupFooters;
           grid.ShowColumnHeaders = source.ShowColumnHeaders;
           grid.Width = source.ActualWidth;
           if (source.DataContext is BusinessObjectGridViewModel)
           {
               GridColumnCreator columnCreator = new GridColumnCreator(source.DataContext as BusinessObjectGridViewModel, grid);
               columnCreator.CreateColumns();
               grid.ColumnWidth = GridViewLength.Auto;
               foreach (Telerik.Windows.Controls.GridViewColumn column in grid.Columns)
               {
                   column.Width = GridViewLength.Auto;
                   column.MaxWidth = extentSize.Width;
               }
           }
           else
           {
               foreach (var column in source.Columns.OfType<GridViewDataColumn>())
               {
                   var newColumn = new GridViewDataColumn();
                   newColumn.Header = column.Header;
                   newColumn.DataMemberBinding = new System.Windows.Data.Binding(column.UniqueName);
                   newColumn.Width = column.Width;
                   grid.Columns.Add(newColumn);
               }
               grid.SortDescriptors.AddRange(source.SortDescriptors);
               grid.GroupDescriptors.AddRange(source.GroupDescriptors);
               grid.FilterDescriptors.AddRange(source.FilterDescriptors);
           }
           grid.DataContext = source.DataContext;
           StyleManager.SetTheme(grid, StyleManager.GetTheme(grid));
           return grid;
       }

 

2 Answers, 1 is accepted

Sort by
0
Yavor Georgiev
Telerik team
answered on 23 Jul 2010, 08:55 AM
Hello frdotnet,

 In your PaginateGridInToFixedDocument method, you check if a grid is too big in height and create a new page if it is. You just need to do the same thing for the width of the print-friendly RadGridView.

Sincerely yours,
Yavor Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
frdotnet
Top achievements
Rank 1
answered on 28 Jul 2010, 01:19 AM
Thanks for your reply. But somehow , the width of the grid is not getting autosized to display all the columns. The width of the grid is remaining fixed even after auto fitting the columns. Can you please give me some pointers.
Tags
GridView
Asked by
frdotnet
Top achievements
Rank 1
Answers by
Yavor Georgiev
Telerik team
frdotnet
Top achievements
Rank 1
Share this question
or