Results 1 to 23 of 23

Thread: Imprimindo o conteúdo do DataGridView - VB.Net

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2018
    Posts
    33

    Imprimindo o conteúdo do DataGridView - VB.Net

    Hello guys!

    I have a DataGridView which, depending on the user's choice, how the calls change.

    Example: I have a table that has Code, Name, Address and Birth.

    - If the user chooses "Address", my DGV will show the Code, Name and Address columns (... and print these 3 columns only with all the records)

    - If the user chooses "Birth", my DGV will show the Code, Name and Birth columns (... and print these 3 columns only with all the records)

    Could you help me with that?

    Thank you very much and I await your help.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Imprimindo o conteúdo do DataGridView - VB.Net

    Help you with what exactly? Are you asking how to hide and show those columns based on the user's selection? If so, how exactly are they making that selection? If not, what are you asking? If you're asking how to print the contents of a DataGridView, which you mentioned in another thread, then the bit about the changing columns seems to be a bit of a red herring, because printing would generally be based on what is displayed, however that happened to get there.

    If you want to print the grid, visit the VB.NET CodeBank forum on this site and find threads posted by Merrion. One of them is dedicated to a DataGridPrinter class. That's the old WinForms DataGrid that was the only option at the time. Later in the thread, a similar DataGridViewPrinter class is provided, which would suit your needs. I believe that .paul. also has a similar class posted off-site somewhere, so you may be able to find a link in his signature.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2018
    Posts
    33

    Re: Printing DataGridView Contents - VB.Net

    Quote Originally Posted by jmcilhinney View Post
    Help you with what exactly? Are you asking how to hide and show those columns based on the user's selection? If so, how exactly are they making that selection? If not, what are you asking? If you're asking how to print the contents of a DataGridView, which you mentioned in another thread, then the bit about the changing columns seems to be a bit of a red herring, because printing would generally be based on what is displayed, however that happened to get there.

    If you want to print the grid, visit the VB.NET CodeBank forum on this site and find threads posted by Merrion. One of them is dedicated to a DataGridPrinter class. That's the old WinForms DataGrid that was the only option at the time. Later in the thread, a similar DataGridViewPrinter class is provided, which would suit your needs. I believe that .paul. also has a similar class posted off-site somewhere, so you may be able to find a link in his signature.

    Hmmm ... that's not what I'm after. Here it prints fixed columns.

    What I want is something very simple, but I do not know how to apply it: Suppose I have a database with the fields "Code", "Name", "Address" and "Birth":

    - When the user selects the "Address" option, the DataGridView "hides" the "Birth" field and only the "Code", "Name" and "Address" columns appear. The same happens when "Birth" is selected, which "hides" the "Address" field .... and so on ... That's what my system is already doing.

    - What I need: That just prints according to what is shown in the DataGridView (... according to what the user selected).

    - Select Address? Print (PDF or A4): Code, Name and Address.
    - Did you select Birth? Print: Code, Name and Birth

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Imprimindo o conteúdo do DataGridView - VB.Net

    So did you test the DataGridViewPrinter class that I directed you to? Are you saying that it will print all the columns? If so, just change it to print only those columns for which the Visible property is set to True.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2018
    Posts
    33

    Re: Imprimindo o conteúdo do DataGridView - VB.Net

    I'm in it, but I'm finding it very complex because I'm still a beginner in VB. I've been told that ReportViewer is fast, but I do not know how to apply it. I searched for several references on the net, but could not apply.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Imprimindo o conteúdo do DataGridView - VB.Net

    That's not going to help you print a DataGridView. A ReportViewer control will print the contents of a report, so you'd have to design a report and load that into the control and then print the report.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Member
    Join Date
    Dec 2018
    Posts
    33

    Re: Printing DataGridView Contents - VB.Net

    Quote Originally Posted by jmcilhinney View Post
    That's not going to help you print a DataGridView. A ReportViewer control will print the contents of a report, so you'd have to design a report and load that into the control and then print the report.
    So I would have to create 1 RLDC for each report I need ... Would that be it?

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Imprimindo o conteúdo do DataGridView - VB.Net

    To print a DataGridView...

    VB.Net Printing Example

    More advanced method - ExtendedDataGridView Control

  9. #9

    Thread Starter
    Member
    Join Date
    Dec 2018
    Posts
    33

    Re: Imprimindo o conteúdo do DataGridView - VB.Net

    Giving a search on the net, I found something very simple that could help:

    Code:
    Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            Dim ce = Form1.grid.CurrentCell.RowIndex
            e.Graphics.DrawString(Form1.grid.Item(Form1.Column1.HeaderText, ce).Value.ToString, SystemFonts.DefaultFont, Brushes.Black, 300, 200)
    
            Dim ce1 = Form1.grid.CurrentCell.RowIndex
            e.Graphics.DrawString(Form1.grid.Item(Form1.Column2.HeaderText, ce1).Value.ToString, SystemFonts.DefaultFont, Brushes.Black, 400, 200)
        End Sub
    In this video: https://www.youtube.com/watch?v=-xP3qzxJT8s

    The problem is that:
    - it does not print the whole table ... Just the selected line (... here I know it's due to be the "CurrentCell", but I do not know which one to use to print all;
    - it only prints when I manually type in the DataGrid, but if I collect the information from the database, it fails.

    My DataGrid is already fed with information from the database and the filter I want.

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Imprimindo o conteúdo do DataGridView - VB.Net

    Quote Originally Posted by binhofaa View Post
    Giving a search on the net, I found something very simple that could help:

    Code:
    Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            Dim ce = Form1.grid.CurrentCell.RowIndex
            e.Graphics.DrawString(Form1.grid.Item(Form1.Column1.HeaderText, ce).Value.ToString, SystemFonts.DefaultFont, Brushes.Black, 300, 200)
    
            Dim ce1 = Form1.grid.CurrentCell.RowIndex
            e.Graphics.DrawString(Form1.grid.Item(Form1.Column2.HeaderText, ce1).Value.ToString, SystemFonts.DefaultFont, Brushes.Black, 400, 200)
        End Sub
    In this video: https://www.youtube.com/watch?v=-xP3qzxJT8s

    The problem is that:
    - it does not print the whole table ... Just the selected line (... here I know it's due to be the "CurrentCell", but I do not know which one to use to print all;
    - it only prints when I manually type in the DataGrid, but if I collect the information from the database, it fails.

    My DataGrid is already fed with information from the database and the filter I want.
    How many columns does your DGV have? How many rows? Will it always fit on one sheet of A4?
    I'll wite you a simple example if you answer those three questions...

  11. #11

    Thread Starter
    Member
    Join Date
    Dec 2018
    Posts
    33

    Re: Imprimindo o conteúdo do DataGridView - VB.Net

    How many columns does your DGV have? (For printing only 3, but the total DGV has a 4 columns. Everything will depend on the example I put in post # 1

    How many lines? (Undefined, because it will depend on the filter that was used to select officers).

    Will it always fit on an A4 sheet? (It may be that, as it may not, as I said, it all depends on the amount of staff being filtered)

    I'll give you a simple example if you answer these three questions ... (Great ... I look forward to your next post)

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Imprimindo o conteúdo do DataGridView - VB.Net

    This will work. Read the comments in the code. The text alignment is set to horizontal centre and vertical centre...

    Code:
    Public Class Form1
    
        Private WithEvents pd As New Printing.PrintDocument
        Private ppd As New PrintPreviewDialog
    
        Dim ignoreColumn As Integer = 2 ' index of column to not print
        Dim startRow As Integer
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'for example i added some data
            DataGridView1.Rows.Add(100)
            For x As Integer = 1 To 100
                DataGridView1.Rows(x - 1).Cells(0).Value = x
            Next
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'preview button
            startRow = 0
            ppd.Document = pd
            ppd.WindowState = FormWindowState.Maximized
            ppd.ShowDialog()
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            'print button
            startRow = 0
            pd.Print()
        End Sub
    
        Private Sub pd_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pd.PrintPage
            Dim startX As Integer = e.MarginBounds.Left
            Dim startY As Integer = e.MarginBounds.Top
            Dim r As Rectangle
    
            'this is the text alignment
            Dim sf As New StringFormat
            sf.Alignment = StringAlignment.Center
            sf.LineAlignment = StringAlignment.Center
    
            If startRow = 0 Then
                For x As Integer = 0 To DataGridView1.Columns.Count - 1
                    If x = ignoreColumn Then Continue For
                    r.X = startX
                    r.Y = startY
                    r.Width = DataGridView1.Columns(x).Width
                    r.Height = DataGridView1.Rows(0).Height
                    e.Graphics.DrawRectangle(Pens.Black, r)
                    e.Graphics.DrawString(DataGridView1.Columns(x).HeaderText, DataGridView1.Font, Brushes.Black, r, sf)
                    startX += r.Width
                Next
    
                startY += r.Height
            End If
    
            For y As Integer = startRow To DataGridView1.Rows.Count - 1
                If y = DataGridView1.NewRowIndex Then Continue For
                startX = e.MarginBounds.Left
                For x As Integer = 0 To DataGridView1.Columns.Count - 1
                    If x = ignoreColumn Then Continue For
                    r.X = startX
                    r.Y = startY
                    r.Width = DataGridView1.Columns(x).Width
                    r.Height = DataGridView1.Rows(0).Height
                    e.Graphics.DrawRectangle(Pens.Black, r)
                    e.Graphics.DrawString(If(Not DataGridView1.Rows(y).Cells(x).Value Is Nothing, DataGridView1.Rows(y).Cells(x).Value.ToString, ""), _
                                            DataGridView1.Font, Brushes.Black, r, sf)
    
                    startX += r.Width
                Next
                startY += r.Height
                If startY >= e.MarginBounds.Bottom Then
                    If y < DataGridView1.Rows.Count - 1 Then
                        e.HasMorePages = True
                        startRow = y + 1
                        Exit For
                    End If
                End If
            Next
        End Sub
        
    End Class

  13. #13

    Thread Starter
    Member
    Join Date
    Dec 2018
    Posts
    33

    Re: Imprimindo o conteúdo do DataGridView - VB.Net

    Quote Originally Posted by .paul. View Post
    This will work. Read the comments in the code. The text alignment is set to horizontal centre and vertical centre...

    Code:
    Public Class Form1
    
        Private WithEvents pd As New Printing.PrintDocument
        Private ppd As New PrintPreviewDialog
    
        Dim ignoreColumn As Integer = 2 ' index of column to not print
        Dim startRow As Integer
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'for example i added some data
            DataGridView1.Rows.Add(100)
            For x As Integer = 1 To 100
                DataGridView1.Rows(x - 1).Cells(0).Value = x
            Next
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'preview button
            startRow = 0
            ppd.Document = pd
            ppd.WindowState = FormWindowState.Maximized
            ppd.ShowDialog()
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            'print button
            startRow = 0
            pd.Print()
        End Sub
    
        Private Sub pd_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pd.PrintPage
            Dim startX As Integer = e.MarginBounds.Left
            Dim startY As Integer = e.MarginBounds.Top
            Dim r As Rectangle
    
            'this is the text alignment
            Dim sf As New StringFormat
            sf.Alignment = StringAlignment.Center
            sf.LineAlignment = StringAlignment.Center
    
            If startRow = 0 Then
                For x As Integer = 0 To DataGridView1.Columns.Count - 1
                    If x = ignoreColumn Then Continue For
                    r.X = startX
                    r.Y = startY
                    r.Width = DataGridView1.Columns(x).Width
                    r.Height = DataGridView1.Rows(0).Height
                    e.Graphics.DrawRectangle(Pens.Black, r)
                    e.Graphics.DrawString(DataGridView1.Columns(x).HeaderText, DataGridView1.Font, Brushes.Black, r, sf)
                    startX += r.Width
                Next
    
                startY += r.Height
            End If
    
            For y As Integer = startRow To DataGridView1.Rows.Count - 1
                If y = DataGridView1.NewRowIndex Then Continue For
                startX = e.MarginBounds.Left
                For x As Integer = 0 To DataGridView1.Columns.Count - 1
                    If x = ignoreColumn Then Continue For
                    r.X = startX
                    r.Y = startY
                    r.Width = DataGridView1.Columns(x).Width
                    r.Height = DataGridView1.Rows(0).Height
                    e.Graphics.DrawRectangle(Pens.Black, r)
                    e.Graphics.DrawString(If(Not DataGridView1.Rows(y).Cells(x).Value Is Nothing, DataGridView1.Rows(y).Cells(x).Value.ToString, ""), _
                                            DataGridView1.Font, Brushes.Black, r, sf)
    
                    startX += r.Width
                Next
                startY += r.Height
                If startY >= e.MarginBounds.Bottom Then
                    If y < DataGridView1.Rows.Count - 1 Then
                        e.HasMorePages = True
                        startRow = y + 1
                        Exit For
                    End If
                End If
            Next
        End Sub
        
    End Class
    Hello .paul.

    I tried to apply to my project but failed. The report came in blank. As I am still learning VB, I found it a little too complex for a beginner, but it was worth his intention.

    Walking the net, I found the video below where it shows exactly what I need.

    https://www.youtube.com/watch?v=lA4p3vxiBoM

    I also found the language and the application difficult, but since I have a certain urgency to deliver the work, I ended up doing it the way he presented it. I believe that by PrintDocuments maybe it would be easier the way I mentioned in post # 9, but that way it only prints a record only and not the complete grid.

    If anyone has a simpler suggestion, I would greatly appreciate it.

    Hugs

  14. #14
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    Re: Imprimindo o conteúdo do DataGridView - VB.Net

    Paul has provided you with a complete example, all you have to do is replace DataGridView1 with the name of your DGV, using the PrintDocument doesn't get any easier than that. Personally I would create a RDLC, I've always liked working with report designers, much nicer looking reports with a lot less coding. But NOTHING is going to be EASY until you take the time to learn how to do it.

  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Imprimindo o conteúdo do DataGridView - VB.Net

    If you still can't implement it, post your full current form code, and i'll show you how to use my printing code with your dgv.

  16. #16
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    Re: Imprimindo o conteúdo do DataGridView - VB.Net

    Quote Originally Posted by .paul. View Post
    If you still can't implement it, post your full current form code, and i'll show you how to use my printing code with your dgv.
    Isn't it @ 3 in the morning where you live???

  17. #17

    Thread Starter
    Member
    Join Date
    Dec 2018
    Posts
    33

    Re: Imprimindo o conteúdo do DataGridView - VB.Net

    Quote Originally Posted by .paul. View Post
    If you still can't implement it, post your full current form code, and i'll show you how to use my printing code with your dgv.
    Here is my code:

    Code:
    Public Class FRM_Print
        Private WithEvents pd As New Printing.PrintDocument
        Private ppd As New PrintPreviewDialog
    
        Dim ignoreColumn As Integer = 2 ' index of column to not print
        Dim startRow As Integer
        Private Sub FRM_Print_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'for example i added some data
            Form1.DGV_Consulta_Periodo.Rows.Add(100)
            For x As Integer = 1 To 100
                Form1.DGV_Consulta_Periodo.Rows(x - 1).Cells(0).Value = x
            Next
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 'Handles Button1.Click
            'preview button
            startRow = 0
            ppd.Document = pd
            ppd.WindowState = FormWindowState.Maximized
            ppd.ShowDialog()
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 'Handles Button2.Click
            'print button
            startRow = 0
            pd.Print()
        End Sub
    
        Private Sub pd_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pd.PrintPage
            Dim startX As Integer = e.MarginBounds.Left
            Dim startY As Integer = e.MarginBounds.Top
            Dim r As Rectangle
    
            'this is the text alignment
            Dim sf As New StringFormat
            sf.Alignment = StringAlignment.Center
            sf.LineAlignment = StringAlignment.Center
    
            If startRow = 0 Then
                For x As Integer = 0 To Form1.DGV_Consulta_Periodo.Columns.Count - 1
                    If x = ignoreColumn Then Continue For
                    r.X = startX
                    r.Y = startY
    
                    r.Width = Form1.DGV_Consulta_Periodo.Columns(x).Width
                    r.Height = Form1.DGV_Consulta_Periodo.Rows(0).Height
                    e.Graphics.DrawRectangle(Pens.Black, r)
                    e.Graphics.DrawString(Form1.DGV_Consulta_Periodo.Columns(x).HeaderText, Form1.DGV_Consulta_Periodo.Font, Brushes.Black, r, sf)
                    startX += r.Width
                Next
    
                startY += r.Height
            End If
    
            For y As Integer = startRow To Form1.DGV_Consulta_Periodo.Rows.Count - 1
                If y = Form1.DGV_Consulta_Periodo.NewRowIndex Then Continue For
                startX = e.MarginBounds.Left
                For x As Integer = 0 To Form1.DGV_Consulta_Periodo.Columns.Count - 1
                    If x = ignoreColumn Then Continue For
                    r.X = startX
                    r.Y = startY
                    r.Width = Form1.DGV_Consulta_Periodo.Columns(x).Width
                    r.Height = Form1.DGV_Consulta_Periodo.Rows(0).Height
                    e.Graphics.DrawRectangle(Pens.Black, r)
                    e.Graphics.DrawString(If(Not Form1.DGV_Consulta_Periodo.Rows(y).Cells(x).Value Is Nothing, Form1.DGV_Consulta_Periodo.Rows(y).Cells(x).Value.ToString, ""),
                                            Form1.DGV_Consulta_Periodo.Font, Brushes.Black, r, sf)
    
                    startX += r.Width
                Next
                startY += r.Height
                If startY >= e.MarginBounds.Bottom Then
                    If y < Form1.DGV_Consulta_Periodo.Rows.Count - 1 Then
                        e.HasMorePages = True
                        startRow = y + 1
                        Exit For
                    End If
                End If
            Next
        End Sub
    End Class

  18. #18
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Imprimindo o conteúdo do DataGridView - VB.Net

    Ok. That's just my code, that you've tried to modify. What is the name of your DataGridView?

  19. #19

    Thread Starter
    Member
    Join Date
    Dec 2018
    Posts
    33

    Re: Imprimindo o conteúdo do DataGridView - VB.Net

    Quote Originally Posted by .paul. View Post
    Ok. That's just my code, that you've tried to modify. What is the name of your DataGridView?
    Yes. I have adapted to the names of my objects. The name of my datagrid is DGV_Consulta_Periodo

  20. #20
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    Re: Imprimindo o conteúdo do DataGridView - VB.Net

    Well, what happened?

  21. #21
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Imprimindo o conteúdo do DataGridView - VB.Net

    Ok i'll put the right names in and break it down and explain where you should put that in your code. I'll be a minute - gotta put my shopping away

  22. #22
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Imprimindo o conteúdo do DataGridView - VB.Net

    Ok. You have a Form, where DGV_Consulta_Periodo resides. In the code for that form, at the very top after Public Class ... etc put this...

    Code:
    Private WithEvents pd As New Printing.PrintDocument
    Private ppd As New PrintPreviewDialog
    
    Dim ignoreColumn As Integer
    Dim startRow As Integer
    Then in the same class, put...

    Code:
    Private Sub pd_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pd.PrintPage
        Dim startX As Integer = e.MarginBounds.Left
        Dim startY As Integer = e.MarginBounds.Top
        Dim r As Rectangle
    
        'this is the text alignment
        Dim sf As New StringFormat
        sf.Alignment = StringAlignment.Center
        sf.LineAlignment = StringAlignment.Center
    
        If startRow = 0 Then
            For x As Integer = 0 To DGV_Consulta_Periodo.Columns.Count - 1
                If x = ignoreColumn Then Continue For
                r.X = startX
                r.Y = startY
    
                r.Width = DGV_Consulta_Periodo.Columns(x).Width
                r.Height = DGV_Consulta_Periodo.Rows(0).Height
                e.Graphics.DrawRectangle(Pens.Black, r)
                e.Graphics.DrawString(DGV_Consulta_Periodo.Columns(x).HeaderText, DGV_Consulta_Periodo.Font, Brushes.Black, r, sf)
                startX += r.Width
            Next
    
            startY += r.Height
        End If
    
        For y As Integer = startRow To DGV_Consulta_Periodo.Rows.Count - 1
            If y = DGV_Consulta_Periodo.NewRowIndex Then Continue For
            startX = e.MarginBounds.Left
            For x As Integer = 0 To Form1.DGV_Consulta_Periodo.Columns.Count - 1
                If x = ignoreColumn Then Continue For
                r.X = startX
                r.Y = startY
                r.Width = DGV_Consulta_Periodo.Columns(x).Width
                r.Height = DGV_Consulta_Periodo.Rows(0).Height
                e.Graphics.DrawRectangle(Pens.Black, r)
                e.Graphics.DrawString(If(Not DGV_Consulta_Periodo.Rows(y).Cells(x).Value Is Nothing, DGV_Consulta_Periodo.Rows(y).Cells(x).Value.ToString, ""), _
                                        DGV_Consulta_Periodo.Font, Brushes.Black, r, sf)
    
                startX += r.Width
            Next
            startY += r.Height
            If startY >= e.MarginBounds.Bottom Then
                If y < DGV_Consulta_Periodo.Rows.Count - 1 Then
                    e.HasMorePages = True
                    startRow = y + 1
                    Exit For
                End If
            End If
        Next
    End Sub
    Also, in that same class, you should have a button or a menuitem to start printing, in the click handler for that button or menuitem, put...

    Code:
    'print button
    ignoreColumn = 2 ' don't print the column at index 2
    'to print all columns use...
    'ignoreColumn = -1
    startRow = 0
    pd.Print()
    if you choose to include a print preview option, use this code...

    Code:
    'preview button
    ignoreColumn = 2 ' don't print the column at index 2
    'to print all columns use...
    'ignoreColumn = -1
    startRow = 0
    ppd.Document = pd
    ppd.WindowState = FormWindowState.Maximized
    ppd.ShowDialog()
    Try that and post back to let us know how it goes. Try to be specific about any errors...

  23. #23
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Imprimindo o conteúdo do DataGridView - VB.Net

    Quote Originally Posted by wes4dbt View Post
    Isn't it @ 3 in the morning where you live???
    Yes it was... I was just about to sleep

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width