簡體   English   中英

在PrintDocument中打印DataGridView

[英]Printing DataGridView in PrintDocument

我是vb.net的新手,我和我的團隊一起在開發一個系統,在該系統中,我們主要使用DataGridView來查看SQL Server數據庫中的記錄。

這是我的問題,我有兩個DataGridViews,其中一個提取ID和學生姓名,而另一個則根據所選的另一個DataGridView行提取記錄(學生的成績)。 我已經可以打印記錄了,但是當我關閉PrintDocument(不退出表單)並選擇另一個學生並在PrintDocument中再次查看它時,

這給我一個錯誤的說法

"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

並且出現后續問題,當我有此代碼"cell.RowIndex - 1" ,無法在PrintDocument中打印DatagridView中的第一條記錄,而當我擦除代碼中的“-1”時,標題變得瘋狂,有時它們變成空白(帶有邊框),有時它們實際上不顯示,僅顯示記錄。

這是發生錯誤的地方:

 e.Graphics.DrawString(DataGridView1.Rows(cell.RowIndex - 1) 
    .Cells(cell.ColumnIndex).FormattedValue.ToString, .Font, 
     Brushes.Black, rc, frmt)

這是我的代碼:

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    With DataGridView1
        Dim frmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
        frmt.LineAlignment = StringAlignment.Center
        frmt.Trimming = StringTrimming.EllipsisCharacter

        Dim HeaderFont As Font = New Drawing.Font("Times New Roman", 20)
        Dim reportFont As Font = New Drawing.Font("Times New Roman", 14)
        Dim nrmlfnt As Font = New Drawing.Font("Calbiri", 10)
        Dim drawBrush As New SolidBrush(Color.Black)
        Dim blackpen As New Pen(Color.Black, 1)

        e.Graphics.DrawString("First Fruits Christian Academy", HeaderFont, drawBrush, 250, 50)
        e.Graphics.DrawString("Purok 17 Hindangon, Valencia City Bukidnon", reportFont, drawBrush, 245, 80)
        e.Graphics.DrawString("Student Grade", reportFont, drawBrush, 370, 125)
        e.Graphics.DrawString("Name: " & txtName.Text & "", nrmlfnt, drawBrush, 100, 180)
        e.Graphics.DrawString("Gender: " & txtGender.Text & "", nrmlfnt, drawBrush, 600, 180)
        e.Graphics.DrawString("Grade & Section: " & cboYearLevel.Text & " - " & cboSection.Text & "", nrmlfnt, drawBrush, 100, 200)

        Dim y As Single = e.MarginBounds.Top + 125
        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 - 20, cell.Size.Height)
                e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height)
                    If (newpage) Then
                        e.Graphics.DrawString(DataGridView1.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, frmt)
                    Else
                    e.Graphics.DrawString(DataGridView1.Rows(cell.RowIndex - 1).Cells(cell.ColumnIndex).FormattedValue.ToString, .Font, Brushes.Black, rc, frmt)
                    End If
                x += rc.Width
                h = Math.Max(h, rc.Height)
            Next
            newpage = False
            y += h
            mRow += 1
            If y + h > e.MarginBounds.Bottom Then
                e.HasMorePages = True
                newpage = True
                Exit Sub
            End If
        Loop
        mRow = 0
    End With
End Sub

DataGridView打印內容的最佳方法是利用String.Format將值連接在一起,將每一行的集合放入List(Of String) 將類變量用於列表中的位置,以使下一頁繼續。

Private index As Integer
Private Sub Print(...) Handles PrintDocument1.PrintPage
   Dim row As Integer = {some point you want to start at}
  'Paint a title - since this event fires for each page
  'continue loop or start loop
  For i As Integer = index To myList.Count - 1
   If Not row = e.MarginBounds.Bottom - 12 Then
    'remember where we are in the list
    index = i
    'paint your contents
   Else
    'start new page
    e.HasMorePages = True
    Exit Sub
   End If
  Next
  'reset the index for next print job
  If Not e.HasMorePages Then index = 0 
 End Sub

Dim myList As New List (Of String)
For Each row In dgv.Rows
  'add what data you want to print
Next

可能您需要從此代碼中刪除Else部分,

If (newpage) Then
    e.Graphics.DrawString(DataGridView1.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, frmt)
Else
    e.Graphics.DrawString(DataGridView1.Rows(cell.RowIndex - 1).Cells(cell.ColumnIndex).FormattedValue.ToString, .Font, Brushes.Black, rc, frmt)
End If

並將其更改為

If (newpage) Then
    e.Graphics.DrawString(DataGridView1.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, frmt)
End If
' and at this point you should re-calculate your rectangle again
e.Graphics.DrawString(DataGridView1.Rows(cell.RowIndex - 1).Cells(cell.ColumnIndex).FormattedValue.ToString, .Font, Brushes.Black, rc, frmt)

關鍵是, 在每次迭代中,您都必須打印該行 ,因此,如果newpage標志為true,則意味着您只需要在打印該行之前首先打印標題即可。

我並證明您的代碼有效,現在的問題是服務器提供了最后一行數據。 我進行了更改以使其起作用:

Dim NewPage As Boolean 
  NewPage = True 

  If (NewPage) = True Then 
    e.Graphics.DrawString (FDesempeño.DataGridView1.Columns (cell.ColumnIndex) .HeaderText, .font, Brushes.Black, rc, frmt) 
 
else 
  e.Graphics.DrawString (FDesempeño.DataGridView1.Rows (cell.RowIndex - 1) .Cells (cell.ColumnIndex) .FormattedValue.ToString, .font, Brushes.Black, rc, frmt) 
End If 

希望您能像我一樣成為新手,找到服務。 如果您可以糾正錯誤(如果我是agradesericia),則可以通過電子郵件與我們聯系,我的電子郵件為pablo_buy@hotmail.com

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM