Печать выделенного диапазона DGV - Visual Basic .NET
Формулировка задачи:
Добрый день, подскажите как в таблице datagrid напечатать выделенный диапазон, не через отчет.
Решение задачи: «Печать выделенного диапазона DGV»
textual
Листинг программы
Public Class Form1 Private start As Integer Private tbb, tb As DataTable Private prtFont As Font = New Font("Arial", 10.0) Private prtPage As Integer = 0 Private maxPages As Integer = 50 Private nn As Integer Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect End Sub Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 'моделирование данных tb = New DataTable() addCols(tb) Dim int1 As New Random Dim int2 As New Random For i = 1 To 100 Dim ss As String() = {i.ToString, int1.Next(100, 999), int1.Next(1000, 9990), Format(int2.NextDouble * 10000, "0.##0")} Dim rr As DataRow = tb.NewRow rr.ItemArray = ss tb.Rows.Add(rr) Next DataGridView1.DataSource = Nothing DataGridView1.DataSource = tb End Sub Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click tbb = New DataTable addCols(tbb) For Each rr As DataGridViewRow In DataGridView1.SelectedRows Dim rw As DataRow = tbb.NewRow rw.ItemArray = (From tt As DataGridViewCell In rr.Cells Select tt.Value).ToArray tbb.Rows.Add(rw) Next nn = 0 If tbb.Rows(0).ItemArray(0) Is DBNull.Value Then nn = 1 start = tbb.Rows.Count - 1 PrintDocument1.Dispose() Using dlg As PrintPreviewDialog = New PrintPreviewDialog dlg.Width = 700 dlg.Height = 500 dlg.UseAntiAlias = True dlg.PrintPreviewControl.Zoom = 1.2 dlg.Document = Me.PrintDocument1 dlg.ShowDialog(Me) End Using End Sub Private Sub addCols(ByRef tb As DataTable) tb.Columns.Add("aaa", System.Type.GetType("System.String")) tb.Columns.Add("bbb", System.Type.GetType("System.String")) tb.Columns.Add("ccc", System.Type.GetType("System.String")) tb.Columns.Add("ddd", System.Type.GetType("System.String")) End Sub Private Sub PrintDocument1_BeginPrint(sender As System.Object, e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint Me.prtPage = 0 start = tbb.Rows.Count - 1 End Sub Private Sub PrintDocument1_EndPrint(sender As System.Object, e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.EndPrint DataGridView1.ClearSelection() End Sub Private Sub PrintDocument1_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage Dim rc As Rectangle = e.MarginBounds rc.Height = (Me.prtFont.Height + 10) Dim ss As String = "" For j = start To nn Step -1 ss = "" For Each s As String In tbb.Rows(j).ItemArray ss &= s.PadRight(10) Next e.Graphics.DrawString(ss, Me.prtFont, Brushes.Black, rc) rc.Y = (rc.Y + rc.Height) If (rc.Bottom > e.MarginBounds.Bottom) Then Me.prtPage += 1 e.HasMorePages = Me.prtPage < maxPages start = j - 1 Return End If Next End Sub End Class
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д