【问题标题】:Trying to print DataGrid View with colored cell backgrounds尝试使用彩色单元格背景打印 DataGridView
【发布时间】:2012-08-20 13:49:27
【问题描述】:

我正在使用免费软件脚本打印 DataGrid 视图,但我面临的问题是,虽然我已为单元格着色,但当我将 DataGrid 传递给 PrintDGV 免费软件代码时,它不会打印颜色。我不熟悉 VB.NET 或 DataGrids,所以这是我第一次尝试这样做。

当前在打印视图中的外观图片:

代码:

http://pastebin.com/0yjvmAp7

最初我以为我可以为 DataGrid 的单元格着色并将其传递给免费软件进行打印,免费软件也会打印背景颜色,但似乎不是这样。..

不确定是不是这样,但你去吧:

Imports System.Collections.Generic

' Print Current Schedule Grid Form
' This form displays a list of the print options.
' When you press "OK", control is returned to the Officer Availability window where the specs are read and the actual printing is done.
' You can choose to limit the number of columns, from the Officer area, that are printed
' You can also choose to limit the # rows that are printed to the range selected in schedule.

Public Class PrintOptions

    ' Form Events

    Public Sub New(ByVal availableFields As List(Of String))

        InitializeComponent()

        ' Add all of the displayed Officer specification columns to the columns available to be printed
        For Each field As String In availableFields
            chklst.Items.Add(field, True)
        Next

    End Sub

    Private Sub PrintOptions_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' Set the Print Defaults - Print all rows by default and fit calendar to page width by default

        rdoAllRows.Checked = True
        chkFitToPageWidth.Checked = False
    End Sub

    ' Form Butten Events

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    ' Read Form Properties

    Public Function GetSelectedColumns() As List(Of String)
        ' Scroll through the list of available columns and return a list of the columns to be printed
        Dim lst As New List(Of String)
        For Each item As Object In chklst.CheckedItems
            lst.Add(item.ToString)
        Next
        Return lst
    End Function

    Public ReadOnly Property PrintTitle() As String
        ' Return the title to be used when printing
        Get
            Return txtTitle.Text
        End Get
    End Property

    Public ReadOnly Property PrintAllRows() As Boolean
        ' Are all rows to be printed or should only the rows selected on the Grid in the Officer Availability window be printed
        Get
            Return rdoAllRows.Checked
        End Get
    End Property

    Public ReadOnly Property FitToPageWidth() As Boolean
        ' Compress grid width to fit on page
        Get
            Return chkFitToPageWidth.Checked
        End Get
    End Property

End Class

【问题讨论】:

    标签: vb.net winforms datagrid datagridview


    【解决方案1】:

    似乎免费软件为您删除了着色(对于打印视图来说这似乎是合法的)。浏览您的代码,我看到调用了 PrintPreviewDialog。看起来这就是魔法发生的地方。你能把代码也贴一下吗?

    【讨论】:

    • 我已经更新了代码。搜索免费软件后,我发现 PrintPreviewDialog 在我粘贴的那个粘贴箱代码中。
    【解决方案2】:

    我通过了解如何更改对我有用的单元格边框颜色解决了这个问题。这是sn-p:

                    ' Initialize the random-number generator.
                    Randomize()
                    ' Generate random value between 1 and 6.
                    MyAlpha = CInt(Int((254 * Rnd()) + 0))
                    ' Initialize the random-number generator.
                    Randomize()
                    ' Generate random value between 1 and 6.
                    MyRed = CInt(Int((254 * Rnd()) + 0))
                    ' Initialize the random-number generator.
                    Randomize()
                    ' Generate random value between 1 and 6.
                    MyGreen = CInt(Int((254 * Rnd()) + 0))
                    ' Initialize the random-number generator.
                    Randomize()
                    ' Generate random value between 1 and 6.
                    MyBlue = CInt(Int((254 * Rnd()) + 0))
    
    
                    Dim penColor As New Pen(Color.FromArgb(MyAlpha, MyRed, MyGreen, MyBlue))
                    ' Drawing Cells Borders 
                    e.Graphics.DrawRectangle(penColor, New Rectangle(ColumnLeft(ColPage, ColAt), tmpTop, ColumnWidth(ColPage, ColAt), CellHeight))
    

    【讨论】:

      猜你喜欢
      • 2014-10-08
      • 1970-01-01
      • 2013-04-12
      • 2014-02-27
      • 2012-02-17
      • 2016-05-27
      • 2015-07-11
      • 2018-06-15
      • 1970-01-01
      相关资源
      最近更新 更多