簡體   English   中英

如何更改特定列的 Datagridview alignment 以進行打印?

[英]How to change in Datagridview alignment of a specific Column for printing?

我為我奇怪的代碼道歉。 我是一個完全的新手,只是在學習東西是如何工作的。

我找到了這段代碼並對其進行了一些更改。

我的問題是:

我以某種方式設法讓所有列 alignment 都正確,但我需要讓我的第一個列“Data1”留下 alignment。 我可以在設置中或單擊按鈕來更改它,但在打印時它與其他的正好對齊。我已經嘗試了所有方法,但我的知識可能只有 2%,所以我真的很感激你的幫助。

我有一個 Button1、DataGridView1、PrintPreviewDialog1、PrintDocument1、PrintDialog1

這是表格的圖片和打印預覽的示例: https://drive.google.com/file/d/1VnUzrM9fgiEcJExrXalo7Uo6XKQwT3Sd/view?usp=sharing

這是我的代碼:


    Private mRow As Integer = 0
    Private newpage As Boolean = True
    Private m_PagesPrinted As Integer = 1

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With DataGridView1
           .Rows.Add("Plan 1", "50", "2,99", "28,40", "170,90")
            .Rows.Add("Plan 27 a", "80", "14,99", "227,85", "1427,05")
            .Rows.Add("Plan 27 b", "808", "47,45", "84,85", "14,05")
            .Rows.Add("Plan 27 c", "80", "12,21", "77,10", "27,05")
            .Rows.Add("Plan 27 d", "470", "14,50", "15,40", "227,99")
            .Rows.Add("Plan 27 e", "2", "99,00", "2,84", "4427,67")
            .Rows.Add("Plan 27 f", "4", "10,00", "9,48", "7,74")
            .Rows.Add("Plan 27 g", "54", "150,50", "46,64", "127,50")
        End With
    End Sub

    Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        newpage = True
        'Dim frm As Form = DirectCast(PrintPreviewDialog1, Form)

        PrintPreviewDialog1.PrintPreviewControl.StartPage = 0
        PrintPreviewDialog1.PrintPreviewControl.Zoom = 0.8
        PrintPreviewDialog1.WindowState = FormWindowState.Maximized

        ' sets it to show '...' for long text
        Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
        fmt.LineAlignment = StringAlignment.Center

        Dim newfmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
        newfmt.LineAlignment = StringAlignment.Near

        'fmt.Trimming = StringTrimming.EllipsisCharacter
        Dim y2 As Int32 = 200
        Dim rc As Rectangle
        Dim x2 As Int32 = 2
        Dim h As Int32 = 200
        Dim row2 As DataGridViewRow

        ' print the header text for a new page
        '   use a grey bg just like the control
        If newpage Then
            row2 = DataGridView1.Rows(mRow)
            x2 = e.MarginBounds.Left
            For Each cell As DataGridViewCell In row2.Cells
                Dim x As Integer = 170
                Dim y As Integer = 360
                Dim xwidth As Integer = 190
                Dim yheight As Integer = 20
                Dim cellwidth As Integer = 300
                Dim cellheight As Integer = 370

                Dim fon As New Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold)

                Dim rect As New Rectangle(x, 100, xwidth, yheight)

                ''##########################################################################
                ''##########################################################################
                Dim rek1 As New Rectangle(40, 350, 750, 20)
                e.Graphics.DrawRectangle(Pens.Black, rek1)

                Dim rek2 As New Rectangle(40, 370, 750, 100)
                e.Graphics.DrawRectangle(Pens.Black, rek2)
                ''##########################################################################
                ''##########################################################################

                e.Graphics.DrawString("Data1                                                                                                      Data2             Data3            Data4                 Data5", fon, Brushes.Black, 42, 351)

            Next
            y2 += h

        End If
        newpage = False


        ' now print the data for each row
        Dim thisNDX As Int32
        For thisNDX = mRow To DataGridView1.RowCount - 1
            ' no need to try to print the new row
            If DataGridView1.Rows(thisNDX).IsNewRow Then Exit For

            row2 = DataGridView1.Rows(thisNDX)
            x2 = e.MarginBounds.Left
            h = 0

            ' reset X for data
            x2 = e.MarginBounds.Left

            ' print the data
            For Each cell As DataGridViewCell In row2.Cells
                If cell.Visible Then
                    rc = New Rectangle(x2 - 20, y2, cell.Size.Width, cell.Size.Height)



                    Select Case DataGridView1.Columns(cell.ColumnIndex).DefaultCellStyle.Alignment
                        Case DataGridViewContentAlignment.BottomRight,
                             DataGridViewContentAlignment.BottomRight
                            fmt.Alignment = StringAlignment.Far
                            rc.Offset(-170, -30)
                        Case DataGridViewContentAlignment.BottomRight,
                            DataGridViewContentAlignment.BottomRight
                            fmt.Alignment = StringAlignment.Far
                        Case Else
                            fmt.Alignment = StringAlignment.Far
                            rc.Offset(-170, -30)
                    End Select

                    e.Graphics.DrawString(cell.FormattedValue.ToString(), DataGridView1.Font, Brushes.Black, rc, fmt)

                    x2 += rc.Width
                    h = Math.Max(h, rc.Height)
                End If

            Next
            y2 += h
            ' next row to print
            mRow = thisNDX + 1

            If y2 + h > 500 Then
                e.HasMorePages = True
                mRow -= 1   'causes last row To rePrint On Next page
                m_PagesPrinted += 1
                newpage = True
                Return
            End If
        Next


    End Sub
    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
        PrintDialog1.Document = PrintDocument1
        PrintDialog1.PrinterSettings = PrintDocument1.PrinterSettings
        PrintDialog1.AllowSomePages = True
        If PrintDialog1.ShowDialog = DialogResult.OK Then
            PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
            PrintDocument1.Print()
        End If
    End Sub
End Class

有人能解釋一下需要更改什么嗎,以便在打印時第一列左對齊,rest 在右側。

提前致謝!!

您的Select Case沒有意義。 您的第一個Case匹配DataGridViewContentAlignment.BottomRight兩次,然后您的第二個案例與相同的值匹配兩次。 在所有情況下,您都將StringFormatAlignment設置為StringAlignment.Far 我認為它應該更像這樣:

Select Case DataGridView1.Columns(cell.ColumnIndex).DefaultCellStyle.Alignment
    Case DataGridViewContentAlignment.TopLeft,
         DataGridViewContentAlignment.MiddleLeft,
         DataGridViewContentAlignment.BottomLeft
        fmt.Alignment = StringAlignment.Near
    Case DataGridViewContentAlignment.TopCenter,
         DataGridViewContentAlignment.MiddleCenter,
         DataGridViewContentAlignment.BottomCenter
        fmt.Alignment = StringAlignment.Center
    Case DataGridViewContentAlignment.TopRight,
         DataGridViewContentAlignment.MiddleRight,
         DataGridViewContentAlignment.BottomRight
        fmt.Alignment = StringAlignment.Far

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM