How to Print/Export a Gridview of Gridlayouts in a ListView

6 posts, 0 answers
  1. Christian
    Christian avatar
    17 posts
    Member since:
    May 2016

    Posted 01 Sep Link to this post

    Hi,

    I've got a gridview which contains a ListView of GridLayouts. In essence the items are made of SimpleListViewVisualItems very similar to the ListView example found in the demo applications for winforms. Instead of using a StackLayoutPanel for presentation, all data is filled into a GridLayout for easier maintenance and readability. How should I proceed to implement export and print functionality?

    I thought of exporting row data to images and exporting those images similar to how it is done in a GridView of ChartElements, but GetAsBitmap returns nothing. Here is the relevant code:

    Dim width = column.Width
    Dim height = grid.TableElement.ChildRowHeight - 40
     
    Dim radListElement = New RadListViewElement()
    Dim gridLayout As GridLayout
    Dim gridLayoutElements = New List(Of RadTextBoxElement)
     
    gridLayout = New GridLayout()
    gridLayout.StretchHorizontally = False
    gridLayout.StretchVertically = False
    gridLayout.Columns.Clear()
    gridLayout.Rows.Clear()
     
    Dim myFixedWidth = 300
    Dim myFixedHeight = 40
     
    gridLayout.Columns.Add(New GridLayoutColumn() With {.SizingType = GridLayoutSizingType.Fixed, .FixedWidth = myFixedWidth})
    gridLayout.Columns.Add(New GridLayoutColumn() With {.SizingType = GridLayoutSizingType.Fixed, .FixedWidth = myFixedWidth})
    gridLayout.Columns.Add(New GridLayoutColumn() With {.SizingType = GridLayoutSizingType.Fixed, .FixedWidth = myFixedWidth})
    gridLayout.Columns.Add(New GridLayoutColumn() With {.SizingType = GridLayoutSizingType.Fixed, .FixedWidth = myFixedWidth})
     
    gridLayout.Rows.Add(New GridLayoutRow() With {.SizingType = GridLayoutSizingType.Fixed, .FixedHeight = myFixedHeight})
    gridLayout.Rows.Add(New GridLayoutRow() With {.SizingType = GridLayoutSizingType.Fixed, .FixedHeight = myFixedHeight})
    gridLayout.Rows.Add(New GridLayoutRow() With {.SizingType = GridLayoutSizingType.Fixed, .FixedHeight = myFixedHeight})
    gridLayout.Rows.Add(New GridLayoutRow() With {.SizingType = GridLayoutSizingType.Fixed, .FixedHeight = myFixedHeight})
    gridLayout.Rows.Add(New GridLayoutRow() With {.SizingType = GridLayoutSizingType.Fixed, .FixedHeight = myFixedHeight})
    gridLayout.Rows.Add(New GridLayoutRow() With {.SizingType = GridLayoutSizingType.Fixed, .FixedHeight = myFixedHeight})
     
    For x As Integer = 0 To gridLayout.Rows.Count * gridLayout.Columns.Count - 1
        Dim child As RadTextBoxElement = CType(GetTextBoxElement(x), RadTextBoxElement)
        child.SetValue(GridLayout.RowIndexProperty, x Mod gridLayout.Rows.Count))
        child.SetValue(GridLayout.ColumnIndexProperty, CInt(Fix(x / gridLayout.Rows.Count)))
        gridLayout.Children.Add(child)
        gridLayoutElements.Add(child)
    Next
     
    radListElement.Children.Add(gridLayout)
     
    Application.DoEvents()
     
    radListElement.InvalidateMeasure(True)
    radListElement.UpdateLayout()
     
    Dim image = radListElement.GetAsBitmapEx(Color.White, 1, New SizeF(width, height))
     
    Dim imageTemp = New RadImageItem()
    imageTemp.Image = image
    imageTemp.Image.Save(".\test" + i.ToString() + ".png")
     
    row.Cells("someColumn").Tag = imageTemp.Image
    i += 1

     

    Private Function GetTextBoxElement(count As Integer) As RadElement
            Dim result As New RadTextBoxElement()
            Dim myText As String = ""
            result.Text = myText
            result.StretchHorizontally = True
            result.StretchVertically = True
            Return result
        End Function

    I am sure I am missing something, but unsure of what it might be.

    Looking forward to your respons

  2. Dimitar
    Admin
    Dimitar avatar
    1415 posts

    Posted 05 Sep Link to this post

    Hi Christian,

    Thank you for writing back.

    From your code, it is not clear to me how you are adding the RadListViewElement and what control is its parent in this case. Could you please specify that? If the parent control is a RadGridView you can use the PrintCellPaint event to paint the custom list in the cell.  

    If you want to export to an image you can use the parent control DrawToBitmap method (only if the control displays the entire data and there are no scrollbars). If you want to use our GetAsBitmap method you need to use RadTextBoxControlElement because RadTextBoxElement contains a default .NET textBox and the text would not be saved. In addition, you need to call the LoadElementTree of the parent control before saving the image.    

    I hope this will be useful. Let me know if you have additional questions.

    Regards,
    Dimitar
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Christian
    Christian avatar
    17 posts
    Member since:
    May 2016

    Posted 07 Sep Link to this post

    Hi Dimitar,

    thank you for your answer.

    The above code is executed before the printing process starts to fill a tag with an image of the respective row, hence the RadListViewElement isn't actually parented to anything. It is just supposed to create the image and vanish afterwards. Which control should I use as I parent? I don't want the user to see the whole preparation process, so helper controls should not be visible if possible.

  5. Dimitar
    Admin
    Dimitar avatar
    1415 posts

    Posted 07 Sep Link to this post

    Hello Christian,

    I was thinking that you are using a grid with custom cells and you want to print it. What is the control that you want to print? How exactly you expect the print page to look like?

    There is no need to show the control to save the image. I have attched my test proejct to show you how this works on my side.

    I am looking forward to your reply.
     
    Regards,
    Dimitar
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
  6. Christian
    Christian avatar
    17 posts
    Member since:
    May 2016

    Posted 07 Sep in reply to Dimitar Link to this post

    Hi Dimitar,

    the issue is almost solved. I've managed to put the ListViewElement inside a RadPanel like this:

    Dim elementContainer = New RadPanel()
    elementContainer.PanelElement.Children.Add(radListElement)
    elementContainer.Width = width
    elementContainer.Height = height
    elementContainer.LoadElementTree()

     

    Now it works. The width needs to be adjusted to the one of the grid when printed. How do I get width and height of rows when printed? The printstyle object of the grid doesn't show any properties I could use.

    Thank you for the project, but when I click on the button it throws an Error in GDI+ from System.Drawing.

    Best regards.

  7. Dimitar
    Admin
    Dimitar avatar
    1415 posts

    Posted 08 Sep Link to this post

    Hello Christian,

    Thank you for writing back.

    By default all rows have the same width and height, you can get it like this: 
    Dim height = radGridView1.TableElement.RowHeight
    Dim width = radGridView1.TableElement.Size.Width
     
    Console.WriteLine(New Size(width, height))

    Let me know if I can assist you further.

    Regards,
    Dimitar
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
Back to Top
UI for WinForms is Visual Studio 2017 Ready