locked
Printing DataRepeater Control

    Question

  • "I just drag & drop customer dataset with Details selected from Datasource Explorer on the DataRepeater control. Now I just try to print populated records from DataRepeater control. Any suggestion? or anyother solution?"
    • Edited by Sharfudeen Saturday, August 01, 2009 9:41 AM Not expressed correctly
    Saturday, August 01, 2009 6:40 AM

Answers

  • Sharfundeen, you can use Printer object to print all the DataRepeater items.  The basic idea is to go through all the DataRepeater items and draw each item to a bitmap, then print the bitmap to the printer.

    Sample code:

        Imports Microsoft.VisualBasic.PowerPacks
        Imports Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6

        Private Sub Print_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            If Me.DataRepeater1.ItemCount > 0 Then
                ' define a VB Power Packs Printer object
                Dim printer As New Printer()

                Dim sz As Size = Me.DataRepeater1.CurrentItem.ClientSize
                Dim w As Integer = sz.Width
                Dim h As Integer = sz.Height

                Dim bm As New Bitmap(w, h)

                Dim x = 0
                Dim y = 100

                Dim n As Integer = Me.DataRepeater1.ItemCount
                For i As Integer = 1 To n
                    ' Set item i be the current item so that it can be visible
                    Me.DataRepeater1.CurrentItemIndex = i - 1

                    ' Paint the content of the current DataRepeater item to a bitmap
                    Dim item As DataRepeaterItem = Me.DataRepeater1.CurrentItem
                    item.DrawToBitmap(bm, item.ClientRectangle)

                    ' if y coordinate > height of the page, start a new page
                    If y + h * 15 + 100 > printer.ScaleHeight Then
                        printer.NewPage()
                        y = 100
                    End If

                    ' Print the bitmap to the printer
                    printer.PaintPicture(bm, x, y)

                    ' Calculate the next left,top position
                    y += h * 15
                Next

                printer.EndDoc()
            End If
        End Sub


    Yunfeng Dong (Microsoft)
    Monday, August 03, 2009 8:56 AM
  • You should set printer.PrintAction = Printing.PrintAction.PrintToPreview as early as possible not after printer.EndDoc(), which is too late. 
    To set the current row in the datarepeater, you can see it in YFDong's sample. See revised example bellow.   

    Private Sub Print_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            If Me.DataRepeater1.ItemCount > 0 Then
                ' define a VB Power Packs Printer object
                Dim printer As New Printer()

             ' Set print to preview  
             printer.PrintAction = Printing.PrintAction.PrintToPreview

                Dim sz As Size = Me.DataRepeater1.CurrentItem.ClientSize
                Dim w As Integer = sz.Width
                Dim h As Integer = sz.Height

                Dim bm As New Bitmap(w, h)

                Dim x = 0
                Dim y = 100
               
                ' Save the current row index here
                Dim oldItemIndex = Me.DataRepeater1.CurrentItemIndex;  

                Dim n As Integer = Me.DataRepeater1.ItemCount
                For i As Integer = 1 To n
                    ' Set item i be the current item so that it can be visible
                    Me.DataRepeater1.CurrentItemIndex = i - 1

                    ' Paint the content of the current DataRepeater item to a bitmap
                    Dim item As DataRepeaterItem = Me.DataRepeater1.CurrentItem
                    item.DrawToBitmap(bm, item.ClientRectangle)

                    ' if y coordinate > height of the page, start a new page
                    If y + h * 15 + 100 > printer.ScaleHeight Then
                        printer.NewPage()
                        y = 100
                    End If

                    ' Print the bitmap to the printer
                    printer.PaintPicture(bm, x, y)

                    ' Calculate the next left,top position
                    y += h * 15
                Next

                printer.EndDoc()

                ' Restore the current row index here 
                Me.DataRepeater1.CurrentItemIndex = oldItemIndex  
            End If
        End Sub


    John Chen -- See my team blog: http://blogs.msdn.com/vsdata. All my posts are provided "AS IS" with no warranties, and confer no rights.
    • Marked as answer by Sharfudeen Tuesday, August 04, 2009 6:13 PM
    Tuesday, August 04, 2009 5:33 PM

All replies

  • Have you tried the PrintForm control comes with the Visual Basic PowerPacks?


    John Chen -- See my team blog: http://blogs.msdn.com/vsdata
    Saturday, August 01, 2009 5:57 PM
  • I read about PrintForm but it is only print one page? I need to print whatever records populated in DataRepeater control, all those records to print. And also the same format?
    Saturday, August 01, 2009 6:14 PM
  • You mean even it is not shown in the form?
    You probably have to write your own code to do so.
    I will ask around to see there is a better solution.  

    John Chen -- See my team blog: http://blogs.msdn.com/vsdata
    Saturday, August 01, 2009 6:21 PM
  • I did not write any code now. I have one DataRepeater control, on to that I placed two bounded textboxes(I drag & drop these textboxes from Datasource explorer, so system automatically generated Code-1). I run the application it is populates 100 records. How can I print these records(100 records)? If we see all those records we needs to scroll DataRepeater control. I'm expecting your sample code. Thanks!

    Code-1 :-

        Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'CustomerDataSet.Customer' table. You can move, or remove it, as needed.
            Me.CustomerTableAdapter.Fill(Me.CustomerDataSet.Customer)
        End Sub

        Private Sub CustomerBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CustomerBindingNavigatorSaveItem.Click
            Me.Validate()
            Me.CustomerBindingSource.EndEdit()
            Me.CustomerTableAdapter.Update(Me.CustomerDataSet.Customer)
        End Sub


    Saturday, August 01, 2009 6:49 PM
  • What I'm exactly mean, I needs to print also scroll part of the DataRepeater control. Can anyone help me please?
    Sunday, August 02, 2009 3:36 AM
  • Sharfundeen, you can use Printer object to print all the DataRepeater items.  The basic idea is to go through all the DataRepeater items and draw each item to a bitmap, then print the bitmap to the printer.

    Sample code:

        Imports Microsoft.VisualBasic.PowerPacks
        Imports Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6

        Private Sub Print_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            If Me.DataRepeater1.ItemCount > 0 Then
                ' define a VB Power Packs Printer object
                Dim printer As New Printer()

                Dim sz As Size = Me.DataRepeater1.CurrentItem.ClientSize
                Dim w As Integer = sz.Width
                Dim h As Integer = sz.Height

                Dim bm As New Bitmap(w, h)

                Dim x = 0
                Dim y = 100

                Dim n As Integer = Me.DataRepeater1.ItemCount
                For i As Integer = 1 To n
                    ' Set item i be the current item so that it can be visible
                    Me.DataRepeater1.CurrentItemIndex = i - 1

                    ' Paint the content of the current DataRepeater item to a bitmap
                    Dim item As DataRepeaterItem = Me.DataRepeater1.CurrentItem
                    item.DrawToBitmap(bm, item.ClientRectangle)

                    ' if y coordinate > height of the page, start a new page
                    If y + h * 15 + 100 > printer.ScaleHeight Then
                        printer.NewPage()
                        y = 100
                    End If

                    ' Print the bitmap to the printer
                    printer.PaintPicture(bm, x, y)

                    ' Calculate the next left,top position
                    y += h * 15
                Next

                printer.EndDoc()
            End If
        End Sub


    Yunfeng Dong (Microsoft)
    Monday, August 03, 2009 8:56 AM
  • Thank you so much Yunfeng Dong! It is pretty good. How can I use printprivew in your code?
    Monday, August 03, 2009 1:43 PM
  • This is what I thought as well but I don't have the code :).  Yfdong is the true expert for data repeater. :)
    John Chen -- See my team blog: http://blogs.msdn.com/vsdata. All my posts are provided "AS IS" with no warranties, and confer no rights.
    Monday, August 03, 2009 5:33 PM
  • One more question, when I'm going to use that code it goes to printer and also DataRepeater control scrolls down. How can I use printpreviewdialog instead and how to stop scroll down?
    Monday, August 03, 2009 6:05 PM
  • I do not think you can stop the scroll down. At the end of the print, yo ucan set the current row back to the original row.
    For the preview, I think you can set the printer.PrintAction = PrintToPreview see ref:
    http://msdn.microsoft.com/en-us/library/system.drawing.printing.printaction.aspx

    John Chen -- See my team blog: http://blogs.msdn.com/vsdata. All my posts are provided "AS IS" with no warranties, and confer no rights.
    Monday, August 03, 2009 10:53 PM
  • Hi John, I put this line after printer.EndDoc()

    printer.PrintAction = Printing.PrintAction.PrintToPreview

    it does not show printpreviewdialog. I dont have an idea how to printpreview and also setting up current row in datarepeater control. Do it for me? please...

    Tuesday, August 04, 2009 6:11 AM
  • You should set printer.PrintAction = Printing.PrintAction.PrintToPreview as early as possible not after printer.EndDoc(), which is too late. 
    To set the current row in the datarepeater, you can see it in YFDong's sample. See revised example bellow.   

    Private Sub Print_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            If Me.DataRepeater1.ItemCount > 0 Then
                ' define a VB Power Packs Printer object
                Dim printer As New Printer()

             ' Set print to preview  
             printer.PrintAction = Printing.PrintAction.PrintToPreview

                Dim sz As Size = Me.DataRepeater1.CurrentItem.ClientSize
                Dim w As Integer = sz.Width
                Dim h As Integer = sz.Height

                Dim bm As New Bitmap(w, h)

                Dim x = 0
                Dim y = 100
               
                ' Save the current row index here
                Dim oldItemIndex = Me.DataRepeater1.CurrentItemIndex;  

                Dim n As Integer = Me.DataRepeater1.ItemCount
                For i As Integer = 1 To n
                    ' Set item i be the current item so that it can be visible
                    Me.DataRepeater1.CurrentItemIndex = i - 1

                    ' Paint the content of the current DataRepeater item to a bitmap
                    Dim item As DataRepeaterItem = Me.DataRepeater1.CurrentItem
                    item.DrawToBitmap(bm, item.ClientRectangle)

                    ' if y coordinate > height of the page, start a new page
                    If y + h * 15 + 100 > printer.ScaleHeight Then
                        printer.NewPage()
                        y = 100
                    End If

                    ' Print the bitmap to the printer
                    printer.PaintPicture(bm, x, y)

                    ' Calculate the next left,top position
                    y += h * 15
                Next

                printer.EndDoc()

                ' Restore the current row index here 
                Me.DataRepeater1.CurrentItemIndex = oldItemIndex  
            End If
        End Sub


    John Chen -- See my team blog: http://blogs.msdn.com/vsdata. All my posts are provided "AS IS" with no warranties, and confer no rights.
    • Marked as answer by Sharfudeen Tuesday, August 04, 2009 6:13 PM
    Tuesday, August 04, 2009 5:33 PM
  • Thanks John. Before going to show printpreview dialog, still it scroll down. Anyway we can set the current row back to the original row!
    Thanks again! Great support!
    :)
    Tuesday, August 04, 2009 6:13 PM
  • Monday, March 08, 2010 2:18 PM