Печать выделенного диапазона DGV - Visual Basic .NET

Узнай цену своей работы

Формулировка задачи:

Добрый день, подскажите как в таблице datagrid напечатать выделенный диапазон, не через отчет.

Решение задачи: «Печать выделенного диапазона DGV»

textual
Листинг программы
  1. Public Class Form1
  2.     Private start As Integer
  3.     Private tbb, tb As DataTable
  4.     Private prtFont As Font = New Font("Arial", 10.0)
  5.     Private prtPage As Integer = 0
  6.     Private maxPages As Integer = 50
  7.     Private nn As Integer
  8.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  9.         DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
  10.     End Sub
  11.  
  12.     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  13.         'моделирование данных
  14.         tb = New DataTable()
  15.         addCols(tb)
  16.         Dim int1 As New Random
  17.         Dim int2 As New Random
  18.         For i = 1 To 100
  19.             Dim ss As String() = {i.ToString, int1.Next(100, 999), int1.Next(1000, 9990), Format(int2.NextDouble * 10000, "0.##0")}
  20.             Dim rr As DataRow = tb.NewRow
  21.             rr.ItemArray = ss
  22.             tb.Rows.Add(rr)
  23.         Next
  24.         DataGridView1.DataSource = Nothing
  25.         DataGridView1.DataSource = tb
  26.     End Sub
  27.  
  28.     Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
  29.         tbb = New DataTable
  30.         addCols(tbb)
  31.         For Each rr As DataGridViewRow In DataGridView1.SelectedRows
  32.             Dim rw As DataRow = tbb.NewRow
  33.             rw.ItemArray = (From tt As DataGridViewCell In rr.Cells Select tt.Value).ToArray
  34.             tbb.Rows.Add(rw)
  35.         Next
  36.         nn = 0
  37.         If tbb.Rows(0).ItemArray(0) Is DBNull.Value Then nn = 1
  38.         start = tbb.Rows.Count - 1
  39.         PrintDocument1.Dispose()
  40.         Using dlg As PrintPreviewDialog = New PrintPreviewDialog
  41.             dlg.Width = 700
  42.             dlg.Height = 500
  43.             dlg.UseAntiAlias = True
  44.             dlg.PrintPreviewControl.Zoom = 1.2
  45.             dlg.Document = Me.PrintDocument1
  46.             dlg.ShowDialog(Me)
  47.         End Using
  48.     End Sub
  49.     Private Sub addCols(ByRef tb As DataTable)
  50.         tb.Columns.Add("aaa", System.Type.GetType("System.String"))
  51.         tb.Columns.Add("bbb", System.Type.GetType("System.String"))
  52.         tb.Columns.Add("ccc", System.Type.GetType("System.String"))
  53.         tb.Columns.Add("ddd", System.Type.GetType("System.String"))
  54.     End Sub
  55.  
  56.     Private Sub PrintDocument1_BeginPrint(sender As System.Object, e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
  57.         Me.prtPage = 0
  58.         start = tbb.Rows.Count - 1
  59.     End Sub
  60.  
  61.     Private Sub PrintDocument1_EndPrint(sender As System.Object, e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.EndPrint
  62.         DataGridView1.ClearSelection()
  63.     End Sub
  64.  
  65.     Private Sub PrintDocument1_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
  66.         Dim rc As Rectangle = e.MarginBounds
  67.         rc.Height = (Me.prtFont.Height + 10)
  68.         Dim ss As String = ""
  69.         For j = start To nn Step -1
  70.             ss = ""
  71.             For Each s As String In tbb.Rows(j).ItemArray
  72.                 ss &= s.PadRight(10)
  73.             Next
  74.             e.Graphics.DrawString(ss, Me.prtFont, Brushes.Black, rc)
  75.             rc.Y = (rc.Y + rc.Height)
  76.             If (rc.Bottom > e.MarginBounds.Bottom) Then
  77.                 Me.prtPage += 1
  78.                 e.HasMorePages = Me.prtPage < maxPages
  79.                 start = j - 1
  80.                 Return
  81.             End If
  82.         Next
  83.     End Sub
  84. End Class

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

15   голосов , оценка 3.8 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы