简体   繁体   English

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

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

I apologize for my weird code.我为我奇怪的代码道歉。 I'm a complete newbie and just learning how stuff works.我是一个完全的新手,只是在学习东西是如何工作的。

I found this code and changed some stuff to it.我找到了这段代码并对其进行了一些更改。

My problem is:我的问题是:

I somehow managed for all columns the alignment to be right but I need for my first Colum "Data1" the alignment to be left.我以某种方式设法让所有列 alignment 都正确,但我需要让我的第一个列“Data1”留下 alignment。 I can change that in the settings or with a click of a button but when printing it's just right aligned with the others.. I have tried everything but my knowledge is maybe 2% so I would really appreciate the help.我可以在设置中或单击按钮来更改它,但在打印时它与其他的正好对齐。我已经尝试了所有方法,但我的知识可能只有 2%,所以我真的很感激你的帮助。

I have a Button1, DataGridView1, PrintPreviewDialog1, PrintDocument1, PrintDialog1我有一个 Button1、DataGridView1、PrintPreviewDialog1、PrintDocument1、PrintDialog1

Here is a picture of the Form and an example of Print-Preview: https://drive.google.com/file/d/1VnUzrM9fgiEcJExrXalo7Uo6XKQwT3Sd/view?usp=sharing这是表格的图片和打印预览的示例: https://drive.google.com/file/d/1VnUzrM9fgiEcJExrXalo7Uo6XKQwT3Sd/view?usp=sharing

This is my Code:这是我的代码:


    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

Can somebody explain me what needs to be changed so when printing then the first column is left aligned and the rest is on the right side..有人能解释一下需要更改什么吗,以便在打印时第一列左对齐,rest 在右侧。

Thanks in advance!!提前致谢!!

Your Select Case doesn't make sense.您的Select Case没有意义。 Your first Case matches DataGridViewContentAlignment.BottomRight twice and then your second case matches the same value twice more.您的第一个Case匹配DataGridViewContentAlignment.BottomRight两次,然后您的第二个案例与相同的值匹配两次。 In all cases, you're setting the Alignment of your StringFormat to StringAlignment.Far .在所有情况下,您都将StringFormatAlignment设置为StringAlignment.Far I would think that it should be more like this:我认为它应该更像这样:

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