vb.netdatagridviewdatagridviewcolumnprint-preview

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. 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.

I have a 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

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..

Thanks in advance!!


Solution

  • Your Select Case doesn't make sense. Your first Case matches DataGridViewContentAlignment.BottomRight twice and then your second case matches the same value twice more. In all cases, you're setting the Alignment of your StringFormat to 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