Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8963787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:26:07+00:00 2026-06-15T16:26:07+00:00

Here is my code: Imports System.Data.OleDb Imports System.Drawing.Printing Namespace Print Public Class Form1 Inherits

  • 0

Here is my code:

Imports System.Data.OleDb
Imports System.Drawing.Printing
Namespace Print
Public Class Form1

    Inherits System.Windows.Forms.Form

    Dim PrintC As PrinterClass
    Dim conn As OleDb.OleDbConnection

    Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\db1.mdb"

    Dim sql As String = String.Empty
    Dim ds As DataSet

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        FillDataGrid()
        '//create printerclass object
        PrintC = New PrinterClass(PrintDocument1, dataGrid)
    End Sub

    Private Sub FillDataGrid()

        Try

            Dim dt As New DataTable
            Dim ds As New DataSet

            ds.Tables.Add(dt)
            Dim da As New OleDbDataAdapter


            con.Open()
            da = New OleDbDataAdapter("SELECT * from klient ", con)

            da.Fill(dt)

            con.Close()

            dataGrid.DataSource = dt.DefaultView

            Dim dTable As DataTable
            For Each dTable In ds.Tables

                Dim dgStyle As DataGridTableStyle = New DataGridTableStyle
                dgStyle.MappingName = dTable.TableName
                dataGrid.TableStyles.Add(dgStyle)

            Next
            ' DataGrid settings
            dataGrid.CaptionText = "TE GJITHE KLIENTET"
            dataGrid.HeaderFont = New Font("Verdana", 12)
            dataGrid.TableStyles(0).GridColumnStyles(0).Width = 60
            dataGrid.TableStyles(0).GridColumnStyles(1).Width = 140
            dataGrid.TableStyles(0).GridColumnStyles(2).Width = 140
            dataGrid.TableStyles(0).GridColumnStyles(3).Width = 140
            dataGrid.TableStyles(0).GridColumnStyles(4).Width = 140
            dataGrid.TableStyles(0).GridColumnStyles(5).HeaderText = ""
            dataGrid.TableStyles(0).GridColumnStyles(5).Width = -1


        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try
    End Sub

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        'create printerclass object
        PrintC = New PrinterClass(PrintDocument1, dataGrid)

        PrintDocument1.Print()
    End Sub

    Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreview.Click

        'create printerclass object
        PrintC = New PrinterClass(PrintDocument1, dataGrid)

        ''preview
        Dim ps As New PaperSize("A4", 840, 1150)
        ps.PaperName = PaperKind.A4
        PrintDocument1.DefaultPageSettings.PaperSize = ps

        PrintPreviewDialog1.WindowState = FormWindowState.Normal
        PrintPreviewDialog1.StartPosition = FormStartPosition.CenterScreen
        PrintPreviewDialog1.ClientSize = New Size(600, 600)

        PrintPreviewDialog1.ShowDialog()

    End Sub


    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        'print grid

        Dim morepages As Boolean = PrintC.Print(e.Graphics)
        If (morepages) Then

            e.HasMorePages = True
        End If

    End Sub

End Class
End Namespace

This is how data looks in DataGrid (that’s perfect)…

enter image description here

and here is how it looks when I click PrintPreview. (I don’t want the time to appear there, the “12:00:00” part. in database the date is stored as Short Date (10-Dec-12)

enter image description here

Can somebody suggest a way around that?

Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.Collections
Imports System.Data
Namespace Print
Public Class PrinterClass
    '//clone of Datagrid
    Dim PrintGrid As Grid

    '//printdocument for initial printer settings
    Private PrintDoc As PrintDocument

    '//defines whether the grid is ordered right to left
    Private bRightToLeft As Boolean

    '//Current Top
    Private CurrentY As Single = 0

    '//Current Left
    Private CurrentX As Single = 0

    '//CurrentRow to print
    Private CurrentRow As Integer = 0

    '//Page Counter
    Public PageCounter As Integer = 0

    '/// <summary>
    '/// Constructor Class
    '/// </summary>
    '/// <param name="pdocument"></param>
    '/// <param name="dgrid"></param>
    Public Sub New(ByVal pdocument As PrintDocument, ByVal dgrid As DataGrid)
        'MyBase.new()

        PrintGrid = New Grid(dgrid)
        PrintDoc = pdocument

        '//The grid columns are right to left
        bRightToLeft = dgrid.RightToLeft = RightToLeft.Yes

        '//init CurrentX and CurrentY
        CurrentY = pdocument.DefaultPageSettings.Margins.Top
        CurrentX = pdocument.DefaultPageSettings.Margins.Left


    End Sub

    Public Function Print(ByVal g As Graphics, ByRef currentX As Single, ByRef currentY As Single) As Boolean

        '//use predefined area
        currentX = currentX
        currentY = currentY

        PrintHeaders(g)

        Dim Morepages As Boolean = PrintDataGrid(g)

        currentY = currentY
        currentX = currentX

        Return Morepages


    End Function

    Public Function Print(ByVal g As Graphics) As Boolean

        CurrentX = PrintDoc.DefaultPageSettings.Margins.Left
        CurrentY = PrintDoc.DefaultPageSettings.Margins.Top
        PrintHeaders(g)
        Return PrintDataGrid(g)
    End Function

    '/// <summary>
    '/// Print the Grid Headers
    '/// </summary>
    '/// <param name="g"></param>
    Private Sub PrintHeaders(ByVal g As Graphics)

        Dim sf As StringFormat = New StringFormat

        '//if we want to print the grid right to left
        If (bRightToLeft) Then

            CurrentX = PrintDoc.DefaultPageSettings.PaperSize.Width - PrintDoc.DefaultPageSettings.Margins.Right
            sf.FormatFlags = StringFormatFlags.DirectionRightToLeft
        Else
            CurrentX = PrintDoc.DefaultPageSettings.Margins.Left
        End If
        Dim i As Integer
        For i = 0 To PrintGrid.Columns - 1

            '//set header alignment
            Select Case (CType(PrintGrid.Headers.GetValue(i), Header).Alignment)
                Case HorizontalAlignment.Left 'left 
                    sf.Alignment = StringAlignment.Near
                Case HorizontalAlignment.Center
                    sf.Alignment = StringAlignment.Center
                Case HorizontalAlignment.Right
                    sf.Alignment = StringAlignment.Far
            End Select


            '//advance X according to order
            If (bRightToLeft) Then


                '//draw the cell bounds (lines) and back color
                g.FillRectangle(New SolidBrush(PrintGrid.HeaderBackColor), CurrentX - PrintGrid.Headers(i).Width, CurrentY, PrintGrid.Headers(i).Width, PrintGrid.Headers(i).Height)
                g.DrawRectangle(New Pen(PrintGrid.LineColor), CurrentX - PrintGrid.Headers(i).Width, CurrentY, PrintGrid.Headers(i).Width, PrintGrid.Headers(i).Height)

                '//draw the cell text
                g.DrawString(PrintGrid.Headers(i).CText, PrintGrid.Headers(i).Font, New SolidBrush(PrintGrid.HeaderForeColor), New RectangleF(CurrentX - PrintGrid.Headers(i).Width, CurrentY, PrintGrid.Headers(i).Width, PrintGrid.Headers(i).Height), sf)
                '//next cell
                CurrentX -= PrintGrid.Headers(i).Width
            Else
                '//draw the cell bounds (lines) and back color
                g.FillRectangle(New SolidBrush(PrintGrid.HeaderBackColor), CurrentX, CurrentY, PrintGrid.Headers(i).Width, PrintGrid.Headers(i).Height)
                g.DrawRectangle(New Pen(PrintGrid.LineColor), CurrentX, CurrentY, PrintGrid.Headers(i).Width, PrintGrid.Headers(i).Height)


                '//draw the cell text
                g.DrawString(PrintGrid.Headers(i).CText, PrintGrid.Headers(i).Font, New SolidBrush(PrintGrid.HeaderForeColor), New RectangleF(CurrentX, CurrentY, PrintGrid.Headers(i).Width, PrintGrid.Headers(i).Height), sf)

                '//next cell
                CurrentX += PrintGrid.Headers(i).Width
            End If
        Next

        '//reset to beginning
        If (bRightToLeft) Then
            '//right align
            CurrentX = PrintDoc.DefaultPageSettings.PaperSize.Width - PrintDoc.DefaultPageSettings.Margins.Right
        Else
            '//left align
            CurrentX = PrintDoc.DefaultPageSettings.Margins.Left
        End If

        '//advance to next row
        CurrentY = CurrentY + CType(PrintGrid.Headers.GetValue(0), Header).Height

    End Sub



    Private Function PrintDataGrid(ByVal g As Graphics) As Boolean
        Dim sf As StringFormat = New StringFormat
        PageCounter = PageCounter + 1

        '//if we want to print the grid right to left
        If (bRightToLeft) Then
            CurrentX = PrintDoc.DefaultPageSettings.PaperSize.Width - PrintDoc.DefaultPageSettings.Margins.Right
            sf.FormatFlags = StringFormatFlags.DirectionRightToLeft
        Else
            CurrentX = PrintDoc.DefaultPageSettings.Margins.Left
        End If
        Dim i As Integer
        For i = CurrentRow To PrintGrid.Rows - 1
            Dim j As Integer
            For j = 0 To PrintGrid.Columns - 1

                '//set cell alignment
                Select Case (PrintGrid.Cell(i, j).Alignment)
                    '//left
                Case HorizontalAlignment.Left
                        sf.Alignment = StringAlignment.Near

                    Case HorizontalAlignment.Center
                        sf.Alignment = StringAlignment.Center


                        '//right
                    Case HorizontalAlignment.Right
                        sf.Alignment = StringAlignment.Far

                End Select

                '//advance X according to order
                If (bRightToLeft) Then
                    '//draw the cell bounds (lines) and back color
                    g.FillRectangle(New SolidBrush(PrintGrid.BackColor), CurrentX - PrintGrid.Cell(i, j).Width, CurrentY, PrintGrid.Cell(i, j).Width, PrintGrid.Cell(i, j).Height)
                    g.DrawRectangle(New Pen(PrintGrid.LineColor), CurrentX - PrintGrid.Cell(i, j).Width, CurrentY, PrintGrid.Cell(i, j).Width, PrintGrid.Cell(i, j).Height)

                    '//draw the cell text
                    g.DrawString(PrintGrid.Cell(i, j).CText, PrintGrid.Cell(i, j).Font, New SolidBrush(PrintGrid.ForeColor), New RectangleF(CurrentX - PrintGrid.Cell(i, j).Width, CurrentY, PrintGrid.Cell(i, j).Width, PrintGrid.Cell(i, j).Height), sf)

                    '//next cell
                    CurrentX -= PrintGrid.Cell(i, j).Width
                Else
                    '//draw the cell bounds (lines) and back color
                    g.FillRectangle(New SolidBrush(PrintGrid.BackColor), CurrentX, CurrentY, PrintGrid.Cell(i, j).Width, PrintGrid.Cell(i, j).Height)
                    g.DrawRectangle(New Pen(PrintGrid.LineColor), CurrentX, CurrentY, PrintGrid.Cell(i, j).Width, PrintGrid.Cell(i, j).Height)
                    '//draw the cell text
                    '//Draw text by alignment
                    g.DrawString(PrintGrid.Cell(i, j).CText, PrintGrid.Cell(i, j).Font, New SolidBrush(PrintGrid.ForeColor), New RectangleF(CurrentX, CurrentY, PrintGrid.Cell(i, j).Width, PrintGrid.Cell(i, j).Height), sf)

                    '//next cell
                    CurrentX += PrintGrid.Cell(i, j).Width
                End If


            Next

            '//reset to beginning
            If (bRightToLeft) Then
                '//right align
                CurrentX = PrintDoc.DefaultPageSettings.PaperSize.Width - PrintDoc.DefaultPageSettings.Margins.Right
            Else
                '//left align
                CurrentX = PrintDoc.DefaultPageSettings.Margins.Left
            End If

            '//advance to next row
            CurrentY += PrintGrid.Cell(i, 0).Height
            CurrentRow += 1
            '//if we are beyond the page margin (bottom) then we need another page,
            '//return true
            If (CurrentY > PrintDoc.DefaultPageSettings.PaperSize.Height - PrintDoc.DefaultPageSettings.Margins.Bottom) Then

                Return True
            End If
        Next
        Return False

    End Function
End Class
End Namespace
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:26:08+00:00Added an answer on June 15, 2026 at 4:26 pm

    Albo,

    I think it might be useful to set a breakpoint at

     '//draw the cell text
     g.DrawString(PrintGrid.Cell(i, j).CText, PrintGrid.Cell(i, j).Font, New SolidBrush(PrintGrid.ForeColor), New RectangleF(CurrentX - PrintGrid.Cell(i, j).Width, CurrentY, PrintGrid.Cell(i, j).Width, PrintGrid.Cell(i, j).Height), sf)
    

    To see exactly what’s going on. Without running such a breakpoint, and from this resource here, my first guess might be something like this:

    If j = PrintGrid.Columns - 1 Then
        Dim pattern As String = "MM-dd-yy"
        If DateTime.TryParseExact(PrintGrid.Cell(i, j).CText, pattern, Nothing, DateTimeStyles.None, PrintGrid.Cell(i, j).CText) Then
        g.DrawString(PrintGrid.Cell(i, j).CText, PrintGrid.Cell(i, j).Font, New SolidBrush(PrintGrid.ForeColor), New RectangleF(CurrentX - PrintGrid.Cell(i, j).Width, CurrentY, PrintGrid.Cell(i, j).Width, PrintGrid.Cell(i, j).Height), sf)
    Else
         g.DrawString(PrintGrid.Cell(i, j).CText, PrintGrid.Cell(i, j).Font, New SolidBrush(PrintGrid.ForeColor), New RectangleF(CurrentX - PrintGrid.Cell(i, j).Width, CurrentY, PrintGrid.Cell(i, j).Width, PrintGrid.Cell(i, j).Height), sf)
    End If
    

    This attempts to parse the text of the column (in your case, the PrintGrid.Columns – 1th cell) as a date, if it succeeds, then it puts the newly formatted text back into the column before it sends it to the print sub. I highly doubt this code would work as-is, but with some debugging and application of correct string to datetime formatting…I think this is the right direction.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my code: Imports System.Data Imports System.Data.OleDb Public Class frmAdd Dim con As
I have the following code : Imports System.Data.OleDb Private Sub getData() Dim connStr As
here is my code:(get file line num and word count) import System.IO import Data.Maybe
Here is my code: import javax.swing.*; import java.awt.*; public class PanelModel { public static
Here is the code: import javax.swing.*; import java.awt.event.*; import java.awt.*; public class TestGrid {
Here I found this code: import java.awt.*; import javax.swing.*; public class FunWithPanels extends JFrame
Here is a simple piece of code: import java.io.*; public class Read { public
Here users browse to the Excel file from there system and then data is
I have the following Haskell code import Data.Int import System.Environment type Coord = (Int16,
Here is the code: import javax.swing.SwingUtilities; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JLabel; import java.awt.event.*;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.