Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So, I know these are really noob questions, but I have two. One of them is that in the current code I cannot figure out how to adjust the cell with with the text wrap for height and was wondering if someone could lead me in the right direction to do that. The other question is that I don't know why this is only showing the first row. Is there something wrong with the loop?

I'm sure I am making it more difficult then necessary just can't find a premade class for this type of thing that will run on my VS2010. Seems there are only C# classes.

Thanks in advance and here is the code section:

VB
'Draw the datagridview with contents and set start point
        Dim x As Integer = e.MarginBounds.Left
        Dim y As Integer = 230

        'Instantiate header count
        Dim j As Integer = 0

        Do While (j < Me.DataGridView1.Columns.Count)

            'Declare new rectangle for datagrid
            Dim rect As Rectangle = New Rectangle(x, y, 165, Me.DataGridView1.ColumnHeadersHeight)

            'Set string format and align
            Dim sf As StringFormat = New StringFormat
            sf.LineAlignment = StringAlignment.Center
            sf.Alignment = StringAlignment.Center

            'Draw and fill the rectangle
            e.Graphics.FillRectangle(Brushes.LightGray, rect)
            e.Graphics.DrawRectangle(Pens.Black, rect)

            'Check to make sure column exists
            If (Not (Me.DataGridView1.Columns(j).HeaderText) Is Nothing) Then

                'Draw column header
                e.Graphics.DrawString(Me.DataGridView1.Columns(j).HeaderText, SystemFonts.DefaultFont, Brushes.Black, rect, sf)

            End If

            'Increment the width of rectanlge and column number
            x = (x + rect.Width)
            j = (j + 1)

        Loop

        'Set the point of rectangle start
        x = e.MarginBounds.Left
        y = (y + Me.DataGridView1.ColumnHeadersHeight)

        'Instantiate row count
        Dim i As Integer = 0

        'Draw rows
        For Each row As DataGridViewRow In Me.DataGridView1.Rows

            'Loop through until counter is equal to number of rows
            Do While (i < Me.DataGridView1.Columns.Count)

                'Identify the cell
                Dim cell As DataGridViewCell

                'Associate cell with row
                cell = row.Cells(i)

                'Create new rectangle
                Dim rect As Rectangle = New Rectangle(x, y, 165, cell.Size.Height)

                'Set string format and align
                Dim sf As StringFormat = New StringFormat
                sf.LineAlignment = StringAlignment.Center
                sf.Alignment = StringAlignment.Center

                'Draw the
                e.Graphics.DrawRectangle(Pens.Black, rect)

                'Draw cell values if there is value
                If (Not (cell.Value) Is Nothing) Then
                    e.Graphics.DrawString(cell.Value.ToString, SystemFonts.DefaultFont, Brushes.Black, rect, sf)
                End If

                'Increment the rectangle and count
                x = (x + rect.Width)
                i = (i + 1)
            Loop

            x = 180
            y = (y + row.Height)

        Next
Posted
Updated 1-Aug-13 6:17am
v3
Comments
ledtech3 1-Aug-13 15:40pm    
you don't have a datagrid view in your toolbox to drop in (windows forms) ?

Or are you trying to work in WPF?
Edit:
Mabey this will help.
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview(v=vs.90).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
choren84 1-Aug-13 16:16pm    
No I dropped the datagridview from toolbox this is to get it over from the Form to the PrintDocument.
Sergey Alexandrovich Kryukov 1-Aug-13 18:29pm    
Where is your PrintDocument stuff? Generally, don't print DataGridView itself, print the data you use to populate it...
—SA
choren84 1-Aug-13 21:43pm    
Basically, I am using the datagrid as a holder for items I am adding to a shipment and then updating a sql database with the information in the datagrid. Figured it would be easiest to pull the information for the shipping for items that are a part of the shipment from that datagrid.
Sergey Alexandrovich Kryukov 1-Aug-13 22:27pm    
It looks like this information additionally justifies my point...
—SA

1 solution

…Please see my last comment to the question.

Again, don't print DataGridView itself, print the data you use to populate it… The database serves as a data layer. Move data from data layer to UI and to print, not from UI to print. It reduces stiffness of the system, makes it more loosely coupled:
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA
 
Share this answer
 
Comments
choren84 1-Aug-13 22:38pm    
K, thank you sir.
Sergey Alexandrovich Kryukov 1-Aug-13 22:40pm    
You are very welcome.
—SA

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