在Datagridview中打印彩色的行 [英] Print Colored Rows In A Datagridview

查看:133
本文介绍了在Datagridview中打印彩色的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

所以我有2个datagridviews比较它们之间的信息,如果匹配,它将绿色的行涂成绿色,完成所有空白后将红色的行涂成颜色,所以主要的事情是我不知道如何只打印红色行.

这是我正常打印的方法

打印按钮

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.

推荐答案

您发布了很多代码,但其中很多确实不相关.下次您发布问题时,请将其缩小到您遇到的问题.

在该代码块中的某个位置,您正在循环遍历数据网格中的每一行...就在此位附近:
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)
'...


(我个人将使用For Each块而不是Do While,但每个人自己使用)

此时,您正在抓取一行,稍后再逐个单元地进行打印.因此....如果该行的背景颜色为红色,请跳过打印该行的代码.我认为它看起来像这样:


(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


这篇关于在Datagridview中打印彩色的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆