如何从printpreview对话框中删除时间?

时间:2012-12-10 22:28:02

标签: vb.net printing datagrid print-preview

这是我的代码:

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

这是数据在DataGrid中的外观(这是完美的)......

enter image description here

以及点击PrintPreview时的外观。 (我不希望时间出现在那里,“12:00:00”部分。在数据库中,日期存储为短日期(10-Dec-12)

enter image description here

有人可以建议解决这个问题吗?

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

2 个答案:

答案 0 :(得分:1)

ALBO,

我认为在

设置断点可能很有用
 '//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)

准确了解发生了什么。如果没有运行这样的断点,并且从这个资源here,我的第一个猜测可能是这样的:

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

这会尝试将列的文本(在您的情况下,PrintGrid.Columns - 第1个单元格)解析为日期,如果成功,则会将新格式化的文本放回到列中,然后再将其发送到打印子。我非常怀疑这段代码是否会按原样运行,但是通过调试和应用正确的字符串到日期时间格式化......我认为这是正确的方向。

答案 1 :(得分:0)

基于@SacredFaith发布的答案,我解决了这个问题的方式有点不同。 以下是使用字符串拆分的解决方案:

                    '//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)

                    If j = 3 Or j = 4 Then
                        Dim st As String = PrintGrid.Cell(i, j).CText.ToString
                        Dim t As Array = st.Split(" ")
                        g.DrawString(t(0), PrintGrid.Cell(i, j).Font, New SolidBrush(PrintGrid.ForeColor), New RectangleF(CurrentX, 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, CurrentY, PrintGrid.Cell(i, j).Width, PrintGrid.Cell(i, j).Height), sf)
                    End If