Completed
Last Updated: 12 Jul 2016 09:34 by ADMIN
ADMIN
Dess | Tech Support Engineer, Sr.
Created on: 30 May 2016 10:10
Category: GridView
Type: Bug Report
0
FIX. RadGridView - RadGridView.TableElement.TableHeaderHeight is not respected when printing
To reproduce: populate RadGridView with data and use the following code snippet:
Me.RadGridView1.TableElement.TableHeaderHeight = 60
RadGridView1.PrintPreview()

Workaround:

Private Sub RadButton1_Click(sender As Object, e As EventArgs) Handles RadButton1.Click
    Me.RadGridView1.TableElement.TableHeaderHeight = 60
    Dim printStyle As GridPrintStyle = New GridPrintStyle(RadGridView1)
    Dim renderer As CustomTableViewDefinitionPrintRenderer = New CustomTableViewDefinitionPrintRenderer(RadGridView1)
    printStyle.PrintRenderer = renderer
    RadGridView1.PrintStyle = printStyle
    RadGridView1.PrintPreview()
End Sub

Public Class CustomTableViewDefinitionPrintRenderer
Inherits TableViewDefinitionPrintRenderer

    Public Sub New(grid As RadGridView)
        MyBase.New(grid)

    End Sub

    Private Function GetRowLayout(row As GridViewRowInfo, fitWidthMode As PrintFitWidthMode, _
                                  hierarchyIndent As Integer, drawArea As Rectangle) As TableViewRowLayout
        Dim hashKey As Integer = row.ViewTemplate.GetHashCode() + row.HierarchyLevel
        Dim fi As FieldInfo = GetType(TableViewDefinitionPrintRenderer).GetField("rowLayouts", BindingFlags.NonPublic Or BindingFlags.Instance)
        Dim rowLayouts As Dictionary(Of Integer, TableViewRowLayout) = fi.GetValue(Me)
        If rowLayouts.ContainsKey(hashKey) Then
            Return rowLayouts(hashKey)
        End If

        Dim table As GridTableElement = TryCast(row.ViewTemplate.ViewDefinition.CreateViewUIElement(row.ViewInfo), GridTableElement)
        table.Initialize(Me.GridView.GridViewElement, row.ViewInfo)
        table.RowHeight = Me.GridView.TableElement.RowHeight
        table.TableHeaderHeight = Me.GridView.TableElement.TableHeaderHeight
        Me.GridView.ElementTree.ApplyThemeToElement(table)

        Dim rowLayout As New TableViewRowLayout()
        rowLayout.Context = GridLayoutContext.Printer
        rowLayout.Initialize(table)

        Dim systemWidth As Integer = 0

        For Each c As GridViewColumn In rowLayout.RenderColumns
            If TypeOf c Is GridViewRowHeaderColumn OrElse TypeOf c Is GridViewIndentColumn Then
                systemWidth += rowLayout.GetColumnWidth(c)
            End If
        Next

        Me.GridView.BeginUpdate()

        Dim mode As GridViewAutoSizeColumnsMode = rowLayout.ViewTemplate.AutoSizeColumnsMode

        If fitWidthMode = PrintFitWidthMode.FitPageWidth Then
            Dim state As ColumnsState = Me.SaveColumnsState(rowLayout.ViewTemplate)
            rowLayout.ViewTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill
            Dim groupLevel As Integer = If(row.Group IsNot Nothing, row.Group.Level + 1, 0)
            rowLayout.MeasureRow(New SizeF(drawArea.Width + systemWidth - ((row.HierarchyLevel - groupLevel) * hierarchyIndent), drawArea.Height))
            Me.RestoreColumnsState(state)
        Else
            rowLayout.ViewTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.None
            rowLayout.MeasureRow(New SizeF(Me.GridView.Width, Me.GridView.Height))
        End If

        rowLayout.ViewTemplate.AutoSizeColumnsMode = mode

        Me.GridView.EndUpdate(False)

        rowLayouts.Add(hashKey, rowLayout)

        Return rowLayout
    End Function

    Public Overrides Sub DrawPage(traverser As PrintGridTraverser, drawArea As Rectangle, _
                                  graphics As Graphics, settings As GridPrintSettings, pageNumber As Integer)
        Dim fi As FieldInfo = GetType(TableViewDefinitionPrintRenderer).GetField("currentPage", BindingFlags.Instance Or BindingFlags.NonPublic)
        Dim currentPage As Integer = fi.GetValue(Me)
        Dim skipPage As Boolean = currentPage <> pageNumber
        Dim drawAreaHeight As Integer = drawArea.Height
        Dim currentX As Integer = drawArea.X
        Dim currentY As Integer = drawArea.Y

        Dim rowLayout As TableViewRowLayout = Me.GetRowLayout(Me.GridView.MasterView.TableHeaderRow, _
                                                              settings.FitWidthMode, settings.HierarchyIndent, drawArea)
        rowLayout.IgnoreColumnVisibility = settings.PrintHiddenColumns

        Dim systemWidth As Integer = 0

        For Each c As GridViewColumn In rowLayout.RenderColumns
            If TypeOf c Is GridViewRowHeaderColumn OrElse TypeOf c Is GridViewIndentColumn Then
                systemWidth += rowLayout.GetColumnWidth(c)
            End If
        Next

        Dim rowWidth As Integer = CInt(rowLayout.DesiredSize.Width) - systemWidth

        If settings.FitWidthMode = PrintFitWidthMode.NoFitCentered Then
            currentX += ((drawArea.Width - rowWidth) / 2)
        End If

        Dim fi2 As FieldInfo = GetType(TableViewDefinitionPrintRenderer).GetField("firstPage", _
                                                                                  BindingFlags.Instance Or BindingFlags.NonPublic)
        Dim firstPage As Integer = fi2.GetValue(Me)
        If ((Me.GridView.ShowColumnHeaders AndAlso (firstPage AndAlso pageNumber = 1)) OrElse settings.PrintHeaderOnEachPage) _
            AndAlso Not settings.PrintHierarchy Then
            Me.PrintRow(Me.GridView.MasterView.TableHeaderRow, rowLayout, settings, currentX, currentY, graphics, _
                        drawArea)
            Dim rowHeight As Integer = Me.GetDataRowHeight(Me.GridView.MasterView.TableHeaderRow, rowLayout) + Me.GridView.TableElement.RowSpacing
            currentY += rowHeight
            drawAreaHeight -= rowHeight
        End If

        fi2.SetValue(Me, False)
        Dim skipPageCurrentY As Integer = currentY
        Dim row As GridViewRowInfo = Nothing

        If Me.PrintPages.Count > 0 AndAlso Not settings.PrintHierarchy Then
            row = traverser.Current
        End If

        Dim firstRow As Boolean = True

        While traverser.MoveNext()
            If Not (TypeOf traverser.Current Is GridViewDataRowInfo OrElse TypeOf traverser.Current Is GridViewGroupRowInfo OrElse _
                    TypeOf traverser.Current Is GridViewSummaryRowInfo OrElse (TypeOf traverser.Current Is GridViewTableHeaderRowInfo _
                                                                               AndAlso settings.PrintHierarchy)) Then
                Continue While
            End If

            Dim hierarchyRow As GridViewHierarchyRowInfo = TryCast(traverser.Current, GridViewHierarchyRowInfo)

            If hierarchyRow IsNot Nothing AndAlso hierarchyRow.Views.Count > 0 Then
                Select Case settings.ChildViewPrintMode
                    Case ChildViewPrintMode.PrintFirstView
                        hierarchyRow.ActiveView = hierarchyRow.Views(0)
                        Exit Select
                    Case ChildViewPrintMode.PrintCurrentlyActiveView
                        'Do nothing the view is already selected
                        Exit Select
                    Case ChildViewPrintMode.SelectViewToPrint
                        Dim e As New ChildViewPrintingEventArgs(hierarchyRow.Views.IndexOf(hierarchyRow.ActiveView), hierarchyRow)
                        Me.OnChildViewPrinting(e)
                        hierarchyRow.ActiveView = hierarchyRow.Views(e.ActiveViewIndex)
                        Exit Select
                End Select
            End If

            rowLayout = Me.GetRowLayout(traverser.Current, settings.FitWidthMode, settings.HierarchyIndent, drawArea)

            Dim rowHeight As Integer = 0

            If TypeOf traverser.Current Is GridViewGroupRowInfo Then
                rowHeight = Me.GetRowSize(traverser.Current, rowLayout).Height
            Else
                rowHeight = Me.GetDataRowHeight(traverser.Current, rowLayout)
            End If

            If (currentY + rowHeight >= drawArea.Bottom OrElse skipPageCurrentY + rowHeight >= drawArea.Bottom) AndAlso Not firstRow Then
                traverser.MovePrevious()
                skipPageCurrentY = currentY
                skipPage = currentPage <> pageNumber
                fi.SetValue(Me, currentPage + 1)

                If skipPage Then
                    Continue While
                Else
                    Exit While
                End If
            End If

            If TypeOf traverser.Current Is GridViewGroupRowInfo Then
                If settings.PrintGrouping Then
                    If currentPage = pageNumber Then
                        Me.PrintRowWideCell(traverser.Current, rowLayout, settings, currentX, currentY, graphics)
                        currentY += rowHeight + Me.GridView.TableElement.RowSpacing
                    Else
                        skipPageCurrentY += rowHeight + Me.GridView.TableElement.RowSpacing
                    End If
                End If
            Else
                If TypeOf traverser.Current Is GridViewSummaryRowInfo AndAlso Not settings.PrintSummaries Then
                    Continue While
                End If

                If currentPage = pageNumber Then
                    Me.PrintRow(traverser.Current, rowLayout, settings, currentX, currentY, graphics, _
                                drawArea)
                    currentY += rowHeight + Me.GridView.TableElement.RowSpacing
                Else
                    skipPageCurrentY += rowHeight + Me.GridView.TableElement.RowSpacing
                End If
            End If

            If drawAreaHeight < rowHeight AndAlso firstRow Then
                fi.SetValue(Me, currentPage + 1)

                Exit While
            End If

            firstRow = False
        End While

        If Me.PrintPages.Count > 0 AndAlso Not settings.PrintHierarchy Then
            If currentY + Me.GetDataRowHeight(traverser.Current, rowLayout) < drawArea.Bottom OrElse _
                skipPageCurrentY + Me.GetDataRowHeight(traverser.Current, rowLayout) < drawArea.Bottom Then
                fi.SetValue(Me, currentPage + 1)
            End If

            Me.CurrentPrintPage += 1
            Me.CurrentPrintPage = Me.CurrentPrintPage Mod Me.PrintPages.Count

            If Me.CurrentPrintPage > 0 Then
                If row Is Nothing Then
                    traverser.Reset()
                Else
                    traverser.GoToRow(row)
                End If
            End If
        End If
    End Sub
End Class
0 comments