Click here to Skip to main content
15,916,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello, im working on a datagridview print, my code is working but not how i want it to be, i want the position of the datagridview at the bottom of the paper but the code below print it above the paper, please help me. I'm using Visual Studio 2008

VB
' Code for printing DataGridView
Dim leftMargin As Integer = 70
Dim position As Integer = leftMargin
Dim yPosition As Integer
Dim height As Integer = poDG.ColumnHeadersHeight / 2
yPosition = 0

position = leftMargin
yPosition = poDG.ColumnHeadersHeight / 2
For Each dr As DataGridViewColumn In poDG.Columns
    Dim totalWidth As Double = dr.Width
    e.Graphics.FillRectangle(New SolidBrush(Color.LightGray), New Rectangle(position, yPosition, totalWidth, height))
    e.Graphics.DrawRectangle(Pens.Black, New Rectangle(position, yPosition, totalWidth, height))
    e.Graphics.DrawString(dr.HeaderText, New Font("Verdana", 10, FontStyle.Bold), Brushes.Black, position, yPosition)
    position = position + totalWidth
Next

For Each dr As DataGridViewRow In poDG.Rows
    position = leftMargin
    yPosition = yPosition + poDG.ColumnHeadersHeight / 2
    For Each dc As DataGridViewCell In dr.Cells
        Dim totalWidth As Double = dc.OwningColumn.Width
        e.Graphics.FillRectangle(New SolidBrush(Color.White), New Rectangle(position, yPosition, totalWidth, height))
        e.Graphics.DrawRectangle(Pens.Black, New Rectangle(position, yPosition, dc.OwningColumn.Width, height))
        e.Graphics.DrawString(dc.Value, New Font("Verdana", 8, FontStyle.Regular), Brushes.Black, position, yPosition)
        position = position + totalWidth
    Next
Next
Posted
Updated 28-Jul-11 19:35pm
v2

1 solution

nevermind i solved my own question, i'll post the code for future references, hope this helps...

VB
' Code for printing DataGridView
        Dim leftMargin As Integer = 30
        Dim position As Integer = leftMargin
        Dim yPosition As Integer = 329  ' Change value to change the y-position of the DataGridView
        Dim height As Integer = poDG.ColumnHeadersHeight / 2

        position = leftMargin
        yPosition = yPosition + poDG.ColumnHeadersHeight / 2
        For Each dr As DataGridViewColumn In poDG.Columns
            Dim totalWidth As Double = dr.Width
            e.Graphics.FillRectangle(New SolidBrush(Color.White), New Rectangle(position, yPosition, totalWidth, height))
            e.Graphics.DrawRectangle(blackPen, New Rectangle(position, yPosition, totalWidth, height))
            e.Graphics.DrawString(dr.HeaderText, New Font("Times New Roman", 10, FontStyle.Bold Or FontStyle.Italic), Brushes.Black, position, yPosition, fmt)
            position = position + totalWidth
        Next

        For Each dr As DataGridViewRow In poDG.Rows
            position = leftMargin
            yPosition = yPosition + poDG.ColumnHeadersHeight / 2
            For Each dc As DataGridViewCell In dr.Cells
                Dim totalWidth As Double = dc.OwningColumn.Width
                e.Graphics.FillRectangle(New SolidBrush(Color.White), New Rectangle(position, yPosition, totalWidth, height))
                e.Graphics.DrawRectangle(Pens.White, New Rectangle(position, yPosition, dc.OwningColumn.Width, height))
                e.Graphics.DrawString(dc.Value, New Font("Verdana", 8, FontStyle.Regular), Brushes.Black, position, yPosition)
                position = position + totalWidth
            Next
        Next
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900