Monday, September 4, 2017

Cara Print Form di VB.NET

Saya membuat aplikasi penilaian KTSP & K13, kebetulan saya malas mendesign laporan dari Crystal Report. Tutorial ini berisi source code untuk menampilkan laporan dengan komponen di dalam form, komponen yang saya pakai adalah :
  1. Datagridview (GridDataNilaiCopy)
  2. PrintDocument1
  3. PrintPreviewDialog1
Gambar Pendukung : 

Cara Print Form di VB.NET

Ketika di klik tombol cetak, maka hasilnya seperti ini :


Source Code :

Public Sub Cetak_Print()
        Try
            Dim LebarCell As Integer = 50
            Dim LebarAverage As Integer = 60
            GridDataNilaiCopy.Columns.Clear()
            GridDataNilaiCopy.Font = New Font("Trebuchet MS", 10)
            For Each Col As DataGridViewColumn In GridDataNilai.Columns
                GridDataNilaiCopy.Columns.Add(DirectCast(Col.Clone, DataGridViewColumn))
            Next
            For rowIndex As Integer = 0 To (GridDataNilai.Rows.Count - 1)
                GridDataNilaiCopy.Rows.Add(GridDataNilai.Rows(rowIndex).Cells.Cast(Of DataGridViewCell).Select(Function(c) c.Value).ToArray)
            Next
            GridDataNilaiCopy.Columns(0).Width = 50
            GridDataNilaiCopy.Columns(1).Width = 205
            For i = 2 To 6
                GridDataNilaiCopy.Columns(i).Width = LebarCell
            Next
            GridDataNilaiCopy.Columns(7).Width = LebarAverage
            For i = 8 To 12
                GridDataNilaiCopy.Columns(i).Width = LebarCell
            Next
            GridDataNilaiCopy.Columns(13).Width = LebarAverage
            GridDataNilaiCopy.Columns(21).Width = LebarAverage
            GridDataNilaiCopy.Columns(27).Width = LebarAverage
            PrintPreviewDialog1.Document = PrintDocument1
            PrintDocument1.DefaultPageSettings.Landscape = True
            PrintDocument1.DefaultPageSettings.PaperSize = New System.Drawing.Printing.PaperSize("Theo Kertas", 800, 1420)
            PrintPreviewDialog1.ShowDialog()
        Catch ex As Exception
            MsgBox(ex.ToString(), MsgBoxStyle.Critical, "Tidak ditemukan adanya printer!")
        End Try
End Sub
Sekian dan terima kasih.

3 Beri Komentar: