by Suraj babu malla (3 Submissions)
Category: Databases/Data Access/DAO/ADO
Compatability: VB.NET
Difficulty: Unknown Difficulty
Originally Published: Thu 9th March 2006
Date Added: Mon 8th February 2021
Rating: (2 Votes)
Print the display value of DataGridView control also Print preview, Print Setup. For exporting the display value I have recently upload the
'Created By Suraj babu malla
'Created date 2006/9/3
'Country Nepal
'Remark It is a simple code that you can load a Gride by passing 'Query and SQl connection then Print the value of displayed 'Gride
*
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Windows.Forms
Imports System.IO
Imports System.Reflection ' For Missing.Value and BindingFlags
Imports System.Runtime.InteropServices
Public Class frmQueryBuilder
Inherits System.Windows.Forms.Form
Private bindingSource1 As New BindingSource()
Private dataAdapter As New SqlDataAdapter()
<STAThreadAttribute()> _
Public Shared Sub Main()
System.Windows.Forms.Application.Run(New frmQueryBuilder())
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.Load
End Sub
Private Sub GetData(ByVal selectCommand As String)
Me.DataGridView1.DataSource = Me.bindingSource1
Try
Dim connectionString As String = My.Settings.dbCon.ToCharArray
'MessageBox.Show(My.Settings.dbCon.ToString)
Me.dataAdapter = New SqlDataAdapter(selectCommand, connectionString)
Dim commandBuilder As New SqlCommandBuilder(Me.dataAdapter)
Dim table As New System.Data.DataTable()
table.Locale = System.Globalization.CultureInfo.InvariantCulture
Me.dataAdapter.Fill(table)
Me.bindingSource1.DataSource = table
Me.DataGridView1.AutoResizeColumns( _
DataGridViewAutoSizeColumnsMode.AllCells)
Catch ex As SqlException
Me.DataGridView1.DataSource = Nothing
MessageBox.Show("Error No :" & ex.Number & vbCrLf & "Error Description :" & ex.Message)
End Try
End Sub
Private Sub cmdRunQuery_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRunQuery.Click
' Reload the data from the database.
GetData(txtQuery.Text)
End Sub
Private Sub cmdPrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrintPreview.Click
PrintPreviewDialog1.Document = PrintDocument1
PrintDialog1.Document = PrintDocument1
Call PrintPreviewDialog1.ShowDialog()
End Sub
#Region "Printing Code"
Private oStringFormat As StringFormat
Private oStringFormatComboBox As StringFormat
Private oButton As System.Windows.Forms.Button
Private oCheckbox As System.Windows.Forms.CheckBox
Private oComboBox As System.Windows.Forms.ComboBox
Private nTotalWidth As Int16
Private nRowPos As Int16
Private NewPage As Boolean
Private nPageNo As Int16
Private Header As String = "Elite Networks And Communication Pvt Ltd."
Private sUserName As String = "Will"
Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
oStringFormat = New StringFormat
oStringFormat.Alignment = StringAlignment.Near
oStringFormat.LineAlignment = StringAlignment.Center
oStringFormat.Trimming = StringTrimming.EllipsisCharacter
oStringFormatComboBox = New StringFormat
oStringFormatComboBox.LineAlignment = StringAlignment.Center
oStringFormatComboBox.FormatFlags = StringFormatFlags.NoWrap
oStringFormatComboBox.Trimming = StringTrimming.EllipsisCharacter
oButton = New System.Windows.Forms.Button
oCheckbox = New System.Windows.Forms.CheckBox
oComboBox = New System.Windows.Forms.ComboBox
nTotalWidth = 0
For Each oColumn As DataGridViewColumn In DataGridView1.Columns
nTotalWidth += oColumn.Width
Next
nPageNo = 1
NewPage = True
nRowPos = 0
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Static oColumnLefts As New ArrayList
Static oColumnWidths As New ArrayList
Static oColumnTypes As New ArrayList
Static nHeight As Int16
Dim nWidth, i, nRowsPerPage As Int16
Dim nTop As Int16 = e.MarginBounds.Top
Dim nLeft As Int16 = e.MarginBounds.Left
If nPageNo = 1 Then
For Each oColumn As DataGridViewColumn In DataGridView1.Columns
nWidth = CType(Math.Floor(oColumn.Width / nTotalWidth * nTotalWidth * (e.MarginBounds.Width / nTotalWidth)), Int16)
nHeight = e.Graphics.MeasureString(oColumn.HeaderText, oColumn.InheritedStyle.Font, nWidth).Height + 11
oColumnLefts.Add(nLeft)
oColumnWidths.Add(nWidth)
oColumnTypes.Add(oColumn.GetType)
nLeft += nWidth
Next
End If
Do While nRowPos < DataGridView1.Rows.Count - 1
Dim oRow As DataGridViewRow = DataGridView1.Rows(nRowPos)
If nTop + nHeight >= e.MarginBounds.Height + e.MarginBounds.Top Then
DrawFooter(e, nRowsPerPage)
NewPage = True
nPageNo += 1
e.HasMorePages = True
Exit Sub
Else
If NewPage Then
' Draw Header
e.Graphics.DrawString(Header, New System.Drawing.Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, (e.MarginBounds.Left + e.MarginBounds.Right) / 2 - e.Graphics.MeasureString(Header, New System.Drawing.Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Width / 2, e.MarginBounds.Top - e.Graphics.MeasureString(Header, New System.Drawing.Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13)
' Draw Columns
nTop = e.MarginBounds.Top
i = 0
For Each oColumn As DataGridViewColumn In DataGridView1.Columns
e.Graphics.FillRectangle(New SolidBrush(System.Drawing.Color.LightGray), New System.Drawing.Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))
e.Graphics.DrawRectangle(Pens.Black, New System.Drawing.Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))
e.Graphics.DrawString(oColumn.HeaderText, oColumn.InheritedStyle.Font, New SolidBrush(oColumn.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i), nTop, oColumnWidths(i), nHeight), oStringFormat)
i += 1
Next
NewPage = False
End If
nTop += nHeight
i = 0
For Each oCell As DataGridViewCell In oRow.Cells
If oColumnTypes(i) Is GetType(DataGridViewTextBoxColumn) OrElse oColumnTypes(i) Is GetType(DataGridViewLinkColumn) Then
e.Graphics.DrawString(oCell.Value.ToString, oCell.InheritedStyle.Font, New SolidBrush(oCell.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i), nTop, oColumnWidths(i), nHeight), oStringFormat)
ElseIf oColumnTypes(i) Is GetType(DataGridViewButtonColumn) Then
oButton.Text = oCell.Value.ToString
oButton.Size = New Size(oColumnWidths(i), nHeight)
Dim oBitmap As New Bitmap(oButton.Width, oButton.Height)
oButton.DrawToBitmap(oBitmap, New System.Drawing.Rectangle(0, 0, oBitmap.Width, oBitmap.Height))
e.Graphics.DrawImage(oBitmap, New System.Drawing.Point(oColumnLefts(i), nTop))
ElseIf oColumnTypes(i) Is GetType(DataGridViewCheckBoxColumn) Then
oCheckbox.Size = New Size(14, 14)
oCheckbox.Checked = CType(oCell.Value, Boolean)
Dim oBitmap As New Bitmap(oColumnWidths(i), nHeight)
Dim oTempGraphics As Graphics = Graphics.FromImage(oBitmap)
oTempGraphics.FillRectangle(Brushes.White, New System.Drawing.Rectangle(0, 0, oBitmap.Width, oBitmap.Height))
oCheckbox.DrawToBitmap(oBitmap, New System.Drawing.Rectangle(CType((oBitmap.Width - oCheckbox.Width) / 2, Int32), CType((oBitmap.Height - oCheckbox.Height) / 2, Int32), oCheckbox.Width, oCheckbox.Height))
e.Graphics.DrawImage(oBitmap, New System.Drawing.Point(oColumnLefts(i), nTop))
ElseIf oColumnTypes(i) Is GetType(DataGridViewComboBoxColumn) Then
oComboBox.Size = New Size(oColumnWidths(i), nHeight)
Dim oBitmap As New Bitmap(oComboBox.Width, oComboBox.Height)
oComboBox.DrawToBitmap(oBitmap, New System.Drawing.Rectangle(0, 0, oBitmap.Width, oBitmap.Height))
e.Graphics.DrawImage(oBitmap, New System.Drawing.Point(oColumnLefts(i), nTop))
e.Graphics.DrawString(oCell.Value.ToString, oCell.InheritedStyle.Font, New SolidBrush(oCell.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i) + 1, nTop, oColumnWidths(i) - 16, nHeight), oStringFormatComboBox)
ElseIf oColumnTypes(i) Is GetType(DataGridViewImageColumn) Then
Dim oCellSize As System.Drawing.Rectangle = New System.Drawing.Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight)
Dim oImageSize As Size = CType(oCell.Value, Image).Size
e.Graphics.DrawImage(oCell.Value, New System.Drawing.Rectangle(oColumnLefts(i) + CType(((oCellSize.Width - oImageSize.Width) / 2), Int32), nTop + CType(((oCellSize.Height - oImageSize.Height) / 2), Int32), CType(oCell.Value, Image).Width, CType(oCell.Value, Image).Height))
End If
e.Graphics.DrawRectangle(Pens.Black, New System.Drawing.Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))
i += 1
Next
End If
nRowPos += 1
nRowsPerPage += 1
Loop
DrawFooter(e, nRowsPerPage)
e.HasMorePages = False
End Sub
Private Sub DrawFooter(ByVal e As System.Drawing.Printing.PrintPageEventArgs, ByVal RowsPerPage As Int32)
Dim sPageNo As String = nPageNo.ToString + " of " + Math.Ceiling(DataGridView1.Rows.Count / RowsPerPage).ToString
' Right Align - User Name
e.Graphics.DrawString(sUserName, DataGridView1.Font, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(sPageNo, DataGridView1.Font, e.MarginBounds.Width).Width), e.MarginBounds.Top + e.MarginBounds.Height + 7)
' Left Align - Date/Time
e.Graphics.DrawString(Now.ToLongDateString + " " + Now.ToShortTimeString, DataGridView1.Font, Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top + e.MarginBounds.Height + 7)
' Center - Page No. Info
e.Graphics.DrawString(sPageNo, DataGridView1.Font, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(sPageNo, DataGridView1.Font, e.MarginBounds.Width).Width) / 2, e.MarginBounds.Top + e.MarginBounds.Height + 31)
End Sub
Private Sub cmdPageSetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPageSetup.Click
PageSetupDialog1.Document = PrintDocument1
PageSetupDialog1.ShowDialog()
End Sub
Private Sub cmdPrintOption_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrintOption.Click
PrintDialog1.Document = PrintDocument1
PrintDialog1.AllowSomePages = True
PrintDialog1.AllowCurrentPage = True
PrintDialog1.ShowDialog()
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
frmObjectBrowser.ShowDialog()
End Sub
Function QueryValidation(ByVal selectcommand As String) As Boolean
Dim flag As Boolean = True
Try
Dim connectionString As String = My.Settings.dbCon.ToCharArray
'MessageBox.Show(My.Settings.dbCon.ToString)
Me.dataAdapter = New SqlDataAdapter(selectcommand, connectionString)
Dim commandBuilder As New SqlCommandBuilder(Me.dataAdapter)
Dim table As New System.Data.DataTable()
table.Locale = System.Globalization.CultureInfo.InvariantCulture
Me.dataAdapter.Fill(table)
' Me.bindingSource1.DataSource = table
'Me.DataGridView1.AutoResizeColumns( _
' DataGridViewAutoSizeColumnsMode.AllCells)
Catch ex As SqlException
MessageBox.Show("Error No :" & ex.Number & vbCrLf & "Error Description :" & ex.Message)
flag = False
End Try
Return flag
End Function
End Class
No comments have been posted about Print the display value of DataGridView control also Print preview, Print Setup. For exporting the . Why not be the first to post a comment about Print the display value of DataGridView control also Print preview, Print Setup. For exporting the .