|
-
Feb 26th, 2019, 12:15 AM
#1
Thread Starter
Member
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.
-
Feb 26th, 2019, 12:47 AM
#2
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.
-
Feb 26th, 2019, 07:20 AM
#3
Thread Starter
Member
Re: Printing DataGridView Contents - VB.Net
 Originally Posted by jmcilhinney
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
-
Feb 26th, 2019, 07:23 AM
#4
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.
-
Feb 26th, 2019, 07:40 AM
#5
Thread Starter
Member
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.
-
Feb 26th, 2019, 07:44 AM
#6
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.
-
Feb 26th, 2019, 08:00 AM
#7
Thread Starter
Member
Re: Printing DataGridView Contents - VB.Net
 Originally Posted by jmcilhinney
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?
-
Feb 26th, 2019, 11:05 AM
#8
Re: Imprimindo o conteúdo do DataGridView - VB.Net
To print a DataGridView...
VB.Net Printing Example
More advanced method - ExtendedDataGridView Control
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 26th, 2019, 11:59 AM
#9
Thread Starter
Member
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.
-
Feb 26th, 2019, 12:41 PM
#10
Re: Imprimindo o conteúdo do DataGridView - VB.Net
 Originally Posted by binhofaa
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 26th, 2019, 12:52 PM
#11
Thread Starter
Member
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)
-
Feb 26th, 2019, 01:36 PM
#12
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 26th, 2019, 06:46 PM
#13
Thread Starter
Member
Re: Imprimindo o conteúdo do DataGridView - VB.Net
 Originally Posted by .paul.
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
-
Feb 26th, 2019, 08:44 PM
#14
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.
-
Feb 26th, 2019, 09:44 PM
#15
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 26th, 2019, 09:52 PM
#16
Re: Imprimindo o conteúdo do DataGridView - VB.Net
 Originally Posted by .paul.
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???
-
Feb 27th, 2019, 08:14 AM
#17
Thread Starter
Member
Re: Imprimindo o conteúdo do DataGridView - VB.Net
 Originally Posted by .paul.
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
-
Feb 27th, 2019, 10:14 AM
#18
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?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 27th, 2019, 03:15 PM
#19
Thread Starter
Member
Re: Imprimindo o conteúdo do DataGridView - VB.Net
 Originally Posted by .paul.
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
-
Feb 27th, 2019, 03:17 PM
#20
Re: Imprimindo o conteúdo do DataGridView - VB.Net
-
Feb 27th, 2019, 03:28 PM
#21
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 27th, 2019, 03:50 PM
#22
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 27th, 2019, 04:14 PM
#23
Re: Imprimindo o conteúdo do DataGridView - VB.Net
 Originally Posted by wes4dbt
Isn't it @ 3 in the morning where you live???
Yes it was... I was just about to sleep
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|