I'm currently using below code for print all things in data grid view in vb.net 2012. By all things I mean all columns.

Try
            Dim mRow As Integer = 0
            Dim newpage As Boolean = True
            With gdvcussearchcustomer
                Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
                fmt.LineAlignment = StringAlignment.Center
                fmt.Trimming = StringTrimming.EllipsisCharacter
                Dim y As Single = e.MarginBounds.Top
                Do While mRow < .RowCount
                    Dim row As DataGridViewRow = .Rows(mRow)
                    Dim x As Single = e.MarginBounds.Left
                    Dim h As Single = 0
                    For Each cell As DataGridViewCell In row.Cells
                        Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height)
                        e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height)
                        If (newpage) Then
                            e.Graphics.DrawString(gdvcussearchcustomer.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, fmt)
                        Else
                            e.Graphics.DrawString(gdvcussearchcustomer.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt)
                        End If
                        x += rc.Width
                        h = Math.Max(h, rc.Height)
                    Next
                    If newpage Then
                        newpage = False
                    Else
                        mRow += 1
                    End If
                    y += h
                    If y + h > e.MarginBounds.Bottom Then
                        e.HasMorePages = True
                        mRow -= 1
                        newpage = True
                        Exit Sub
                    End If
                Loop
                mRow = 0
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

But Now I want to learn how to print custom column range....
Any one can advice me where I must edit ?

Recommended Answers

All 3 Replies

Well there are different ways of printing certain columns of a dataviewgrid, I think this is what you mean? Here are two links:
http://stackoverflow.com/questions/24058180/how-to-print-selected-column-in-datagridview-in-vb-net
http://www.codeproject.com/Articles/16670/DataGridView-Printing-by-Selecting-Columns-and-Row
the second one displays c# code but can be downloaded for vb.net.
I did it a bit differently because I need to manipulate the columns I want to print. I make columns I don't want to print visible= false and copy the visible ones to a new datagridview. Here I manipulate and than print these out.

Go through two loops
1. by row count
&
2. by selected column count

Muchas gracias!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.