Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Color printing VB.NET , +
Hello Everyone
 
So I have 2 datagridviews that compare information between them and if it does match it colors the rows in green and after its done all the white spaces colors the rows in RED, so the main thing is that i dont know how to print only the RED rows.
 
This is the method that i use to print normally
 
PRINT BUTTON
Dim dlg As New PrintPreviewDialog()
    
            dlg.Document = PrintDocument2
    
            dlg.ShowDialog()
    
            Dim psDlg As New PageSetupDialog
 

DRAW FOOTER
Private Sub DrawFooter(ByVal e As System.Drawing.Printing.PrintPageEventArgs, ByVal RowsPerPage As Int32)
    
            Dim sPageNo As String = nPageNo.ToString + " of " + Math.Ceiling(Emp_attDataGridView.Rows.Count / RowsPerPage).ToString
    
            ' Right Align - User Name
            e.Graphics.DrawString(sUserName, Emp_attDataGridView.Font, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(sPageNo, Emp_attDataGridView.Font, e.MarginBounds.Width).Width), e.MarginBounds.Top + e.MarginBounds.Height + 7)
    
            ' Left Align - Date/Time
            e.Graphics.DrawString(Now.ToLongDateString + " " + Now.ToShortTimeString, Emp_attDataGridView.Font, Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top + e.MarginBounds.Height + 7)
    
            ' Center  - Page No. Info
            e.Graphics.DrawString(sPageNo, Emp_attDataGridView.Font, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(sPageNo, Emp_attDataGridView.Font, e.MarginBounds.Width).Width) / 2, e.MarginBounds.Top + e.MarginBounds.Height + 31)
    
        End Sub
 
PRINT DOCUMENT - PRINT PAGE
Private Sub PrintDocument2_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument2.PrintPage
    
            Static oColumnLefts As New ArrayList
            Static oColumnWidths As New ArrayList
            Static oColumnTypes As New ArrayList
            Static nHeight As Int16
    
            Dim nWidth, i, nRowsPerPage As Int16
            Dim nTop As Int16 = e.MarginBounds.Top
            Dim nLeft As Int16 = e.MarginBounds.Left
    
            If nPageNo = 1 Then
    
                For Each oColumn As DataGridViewColumn In Emp_attDataGridView.Columns
    
                    nWidth = CType(Math.Floor(oColumn.Width / nTotalWidth * nTotalWidth * (e.MarginBounds.Width / nTotalWidth)), Int16)
    
                    nHeight = e.Graphics.MeasureString(oColumn.HeaderText, oColumn.InheritedStyle.Font, nWidth).Height + 11
    
                    oColumnLefts.Add(nLeft)
                    oColumnWidths.Add(nWidth)
                    oColumnTypes.Add(oColumn.GetType)
                    nLeft += nWidth
    
                Next
    
            End If
    
            Do While nRowPos < Emp_attDataGridView.Rows.Count
    
                Dim oRow As DataGridViewRow = Emp_attDataGridView.Rows(nRowPos)
    
                If nTop + nHeight >= e.MarginBounds.Height + e.MarginBounds.Top Then
    
                    DrawFooter(e, nRowsPerPage)
    
                    NewPage = True
                    nPageNo += 1
                    e.HasMorePages = True
                    Exit Sub
    
                Else
    
                    If NewPage Then
    
    ' Draw Header
    e.Graphics.DrawString(Header, New Font(Emp_attDataGridView.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top-e.Graphics.MeasureString(Header, New Font(Emp_attDataGridView.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 45)
                    e.Graphics.DrawString(Header2, New Font(Emp_attDataGridView.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - e.Graphics.MeasureString(Header, New Font(Emp_attDataGridView.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 30)
                    e.Graphics.DrawString(Header3, New Font(Emp_attDataGridView.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - e.Graphics.MeasureString(Header, New Font(Emp_attDataGridView.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 15)
                    e.Graphics.DrawString(Header4, New Font(Emp_attDataGridView.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - e.Graphics.MeasureString(Header, New Font(Emp_attDataGridView.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 0)
                            
                        ' Draw Columns
                        nTop = e.MarginBounds.Top
                        i = 0
                        For Each oColumn As DataGridViewColumn In Emp_attDataGridView.Columns
    
                            e.Graphics.FillRectangle(New SolidBrush(Drawing.Color.LightGray), New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))
                            e.Graphics.DrawRectangle(Pens.Black, New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))
                            e.Graphics.DrawString(oColumn.HeaderText, oColumn.InheritedStyle.Font, New SolidBrush(oColumn.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i), nTop, oColumnWidths(i), nHeight), oStringFormat)
                            i += 1
    
                        Next
                        NewPage = False
    
                    End If
    
                    nTop += nHeight
                    i = 0
                    For Each oCell As DataGridViewCell In oRow.Cells
    
                        If oColumnTypes(i) Is GetType(DataGridViewTextBoxColumn) OrElse oColumnTypes(i) Is GetType(DataGridViewLinkColumn) Then
    
                            e.Graphics.DrawString(oCell.Value.ToString, oCell.InheritedStyle.Font, New SolidBrush(oCell.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i), nTop, oColumnWidths(i), nHeight), oStringFormat)
    
                        ElseIf oColumnTypes(i) Is GetType(DataGridViewButtonColumn) Then
    
                            oButton.Text = oCell.Value.ToString
                            oButton.Size = New Size(oColumnWidths(i), nHeight)
                            Dim oBitmap As New Bitmap(oButton.Width, oButton.Height)
                            oButton.DrawToBitmap(oBitmap, New Rectangle(0, 0, oBitmap.Width, oBitmap.Height))
                            e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))
    
                        ElseIf oColumnTypes(i) Is GetType(DataGridViewCheckBoxColumn) Then
    
                            oCheckbox.Size = New Size(14, 14)
                            oCheckbox.Checked = CType(oCell.Value, Boolean)
                            Dim oBitmap As New Bitmap(oColumnWidths(i), nHeight)
                            Dim oTempGraphics As Graphics = Graphics.FromImage(oBitmap)
                            oTempGraphics.FillRectangle(Brushes.White, New Rectangle(0, 0, oBitmap.Width, oBitmap.Height))
                            oCheckbox.DrawToBitmap(oBitmap, New Rectangle(CType((oBitmap.Width - oCheckbox.Width) / 2, Int32), CType((oBitmap.Height - oCheckbox.Height) / 2, Int32), oCheckbox.Width, oCheckbox.Height))
                            e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))
    
                        ElseIf oColumnTypes(i) Is GetType(DataGridViewComboBoxColumn) Then
    
                            oComboBox.Size = New Size(oColumnWidths(i), nHeight)
                            Dim oBitmap As New Bitmap(oComboBox.Width, oComboBox.Height)
                            oComboBox.DrawToBitmap(oBitmap, New Rectangle(0, 0, oBitmap.Width, oBitmap.Height))
                            e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))
                            e.Graphics.DrawString(oCell.Value.ToString, oCell.InheritedStyle.Font, New SolidBrush(oCell.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i) + 1, nTop, oColumnWidths(i) - 16, nHeight), oStringFormatComboBox)
    
                        ElseIf oColumnTypes(i) Is GetType(DataGridViewImageColumn) Then
    
                            Dim oCellSize As Rectangle = New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight)
                            Dim oImageSize As Size = CType(oCell.Value, Image).Size
                            e.Graphics.DrawImage(oCell.Value, New Rectangle(oColumnLefts(i) + CType(((oCellSize.Width - oImageSize.Width) / 2), Int32), nTop + CType(((oCellSize.Height - oImageSize.Height) / 2), Int32), CType(oCell.Value, Image).Width, CType(oCell.Value, Image).Height))
    
                        End If
    
                        e.Graphics.DrawRectangle(Pens.Black, New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))
    
                        i += 1
    
                    Next
    
                End If
    
                nRowPos += 1
                nRowsPerPage += 1
    
            Loop
    
            DrawFooter(e, nRowsPerPage)
    
            e.HasMorePages = False
    
        End Sub
 
PRINT PAGE - BEGIN PRINT
Private Sub PrintDocument2_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument2.BeginPrint
            oStringFormat = New StringFormat
            oStringFormat.Alignment = StringAlignment.Near
            oStringFormat.LineAlignment = StringAlignment.Center
            oStringFormat.Trimming = StringTrimming.EllipsisCharacter
    
            oStringFormatComboBox = New StringFormat
            oStringFormatComboBox.LineAlignment = StringAlignment.Center
            oStringFormatComboBox.FormatFlags = StringFormatFlags.NoWrap
            oStringFormatComboBox.Trimming = StringTrimming.EllipsisCharacter
    
            oButton = New Button
            oCheckbox = New CheckBox
            oComboBox = New ComboBox
    
            nTotalWidth = 0
            For Each oColumn As DataGridViewColumn In Emp_attDataGridView.Columns
    
                nTotalWidth += oColumn.Width
    
            Next
            nPageNo = 1
            NewPage = True
            nRowPos = 0
    
        End Sub
 
VARIABLES
Private oStringFormat As StringFormat
        Private oStringFormatComboBox As StringFormat
        Private oButton As Button
        Private oCheckbox As CheckBox
        Private oComboBox As ComboBox
    
        Private nTotalWidth As Int16
        Private nRowPos As Int16
        Private NewPage As Boolean
        Private nPageNo As Int16
        Private Header, Header2, Header3, Header4 As String
        Private sUserName As String = "SKILL MAP"
 
Thanks in Advance.
Posted 1-May-12 8:38am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You posted a lot of code and a lot of it really wasn't relevant. Next time you post a question, please narrow it down to just what you are having trouble with.
 
Somewhere in that code block you are looping through each row in the data grid...Right around this bit:
'...
Do While nRowPos < Emp_attDataGridView.Rows.Count
 
               Dim oRow As DataGridViewRow = Emp_attDataGridView.Rows
(nRowPos)
'...
(Personally I'd use a For Each block instead of a Do While, but to each his own)
 
At this point, you are grabbing a row which later on you are printing cell by cell. So....if the row has a red background color, skip the code that prints it. I think it would look something like this:
If oRow.Cells(0).Style.BackColor = Color.Red Then
                Continue Do 
'Continue Do transfers execution immediately to the next iteration of the Do Loop
            End If
  Permalink  

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



Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 1 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100