تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
[سؤال] سؤال حول طباعة محتويات ال DataGrid
#1
لدي مشروع بيانات افراد اكتمل حتي مرحلة البحث فقط استوقفني ال Previewdailog ياريت حد يمدني بالكود الخاص باعداد الصفحة الذي يتضمن اعداد الاعمدة والصفوف والخط الخ




PHP كود :
Try

            
'Set the left margin
            Dim iLeftMargin As Integer = e.MarginBounds.Left
            '
Set the top margin
            Dim iTopMargin 
As Integer e.MarginBounds.Top
            
'Whether more pages have to print or not
            Dim bMorePagesToPrint As Boolean = False
            Dim iTmpWidth As Integer = 0
            printFont = New Font("Arial", 10)
            '
For the first page to print set the cell width and header height
            
If bFirstPage True Then

                
For Each Gridcol As DataGridViewColumn In DataGridView1.Columns
                    iTmpWidth 
CType(Math.Floor(CType(CType(Gridcol.WidthDouble) / CType(iTotalWidthDouble) * CType(iTotalWidthDouble) * (CType(e.MarginBounds.WidthDouble) / CType(iTotalWidthDouble)), Double)), Integer)

                    
iHeaderHeight CType((e.Graphics.MeasureString(Gridcol.HeaderTextGridcol.InheritedStyle.FontiTmpWidth).Height) + 11Integer)

                    
'Save width and height of headers
                    arrColumnLefts.Add(iLeftMargin)
                    arrColumnWidths.Add(iTmpWidth)
                    iLeftMargin = iLeftMargin + iTmpWidth
                Next

            End If

            '
Loop till all the grid rows not get printed
            
While iRow <= DataGridView1.Rows.Count 1

                Dim GridRow 
As DataGridViewRow DataGridView1.Rows(iRow)
                
'Set the cell height
                iCellHeight = GridRow.Height + 5
                Dim iCount As Integer = 0
                '
Check whether the current page settings allo more rows to print

                If 
iTopMargin iCellHeight >= e.MarginBounds.Height e.MarginBounds.Top Then

                    bNewPage 
True
                    bFirstPage 
False
                    bMorePagesToPrint 
True

                    
Exit While

                Else

                    If 
bNewPage True Then

                        
'Draw header
                        e.Graphics.DrawString("نتيجة البحث", New Font(DataGridView1.Font, FontStyle.Regular), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - e.Graphics.MeasureString("Customer Summary", New Font(DataGridView1.Font, FontStyle.Regular), e.MarginBounds.Width).Height - 13)

                        Dim strDate As String = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString()

                        '
Draw Date
                        e
.Graphics.DrawString(strDate, New Font(DataGridView1.FontFontStyle.Bold), Brushes.Blacke.MarginBounds.Left + (e.MarginBounds.Width e.Graphics.MeasureString(strDate, New Font(DataGridView1.FontFontStyle.Regular), e.MarginBounds.Width).Width), e.MarginBounds.Top e.Graphics.MeasureString("Customer Summary", New Font(New Font(DataGridView1.FontFontStyle.Regular), FontStyle.Regular), e.MarginBounds.Width).Height 13)

                        
'Draw Columns
                        iTopMargin = e.MarginBounds.Top

                        For Each GridCol As DataGridViewColumn In DataGridView1.Columns

                            e.Graphics.FillRectangle(New SolidBrush(Color.LightGray), New Rectangle(CType(arrColumnLefts(iCount), Integer), iTopMargin, CType(arrColumnWidths(iCount), Integer), iHeaderHeight))

                            e.Graphics.DrawRectangle(Pens.Green, New Rectangle(CType(arrColumnLefts(iCount), Integer), iTopMargin, CType(arrColumnWidths(iCount), Integer), iHeaderHeight))

                            e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font, New SolidBrush(GridCol.InheritedStyle.ForeColor), New RectangleF(CType(arrColumnLefts(iCount), Integer), iTopMargin, CType(arrColumnWidths(iCount), Integer), iHeaderHeight), strFormat)
                            iCount = iCount + 1

                        Next GridCol

                        bNewPage = False
                        iTopMargin += iHeaderHeight

                    End If
                    iCount = 0

                    '
Draw Columns Contents
                    
For Each Cel As DataGridViewCell In GridRow.Cells

                        
If Not IsDBNull(Cel.ValueThen

                            e
.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font, New SolidBrush(Cel.InheritedStyle.ForeColor), New RectangleF(CType(arrColumnLefts(iCount), Integer), CType(iTopMarginSingle), CType(arrColumnWidths(iCount), Integer), CType(iCellHeightSingle)), strFormat)

                        
End If

                        
'Drawing Cells Borders
                        e.Graphics.DrawRectangle(Pens.Black, New Rectangle(CType(arrColumnLefts(iCount), Integer), iTopMargin, CType(arrColumnWidths(iCount), Integer), iCellHeight))

                        iCount = iCount + 1

                    Next

                End If

                iRow = iRow + 1
                iTopMargin = iTopMargin + iCellHeight

            End While

            '
If more lines exist, print another page.
            If 
bMorePagesToPrint True Then

                e
.HasMorePages True

            
Else

                
e.HasMorePages False

            End 
If

        Catch 
exc As Exception

            MessageBox
.Show(exc.Message"Error"MessageBoxButtons.OKMessageBoxIcon.Error)

        
End Try 

عندما اعمل معاينة للصفحة تطلع البيانات في نصف الصفحة
الرد }}}
تم الشكر بواسطة:
#2
كود :
Imports System.Data.OleDb
Imports System.Data
Imports System.Web
Imports System.Globalization

Public Class PrintDGV
Dim WithEvents PrtDoc As New Drawing.Printing.PrintDocument
Private intRowPos As Int16

Private Sub btnPrintInvoice_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPrintInvoice.Click

Dim PreviewDialog As New PrintPreviewDialog()
PreviewDialog.Document = PrtDoc
PreviewDialog.WindowState = FormWindowState.Maximized
PreviewDialog.PrintPreviewControl.Zoom = 1.0
PreviewDialog.ShowDialog()

End Sub

Private Sub Prtdoc_PrintPage(ByVal sender As System.Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs) Handles PrtDoc.PrintPage

Dim IntLijnteller As Integer = 1
Dim Borstel As New Drawing.SolidBrush(Color.Black)
Dim MyFont As New Font("Bookman Old Style", 12, FontStyle.Regular)
Dim TekstAfmetingen As New SizeF
Dim IntPrintHo As Integer

Static oColumnLefts As New ArrayList
Static oColumnWidths As New ArrayList
Static oColumnTypes As New ArrayList
Static iX As Integer = 0

Dim nTop As Int16 = CShort(e.MarginBounds.Top)
Dim nLeft As Int16 = CShort(e.MarginBounds.Left)
Dim strRef3 As String = ""
Dim strArtikel4 As String = ""
Dim intAantal5 As Integer = 0
Dim dPrijs7 As Decimal
Dim dKorting8 As Decimal
Dim dTotaal10 As Decimal

IntPrintHo = 100
Dim rowNumber As Integer = 1
Dim row As DataGridViewRow

e.Graphics.PageUnit = GraphicsUnit.Millimeter
MyFont = New Font("Bookman Old Style", 12, FontStyle.Regular)

Dim potlood As New Drawing.Pen(Color.DarkBlue)
potlood.Width = 0.1

IntLijnteller = 1
IntPrintHo = 100

For Each row In dgvFacturenInfo.Rows
strRef3 = String.Empty
strArtikel4 = String.Empty
intAantal5 = CInt(Nothing)

'Printing Reference
strRef3 = CStr(dgvFacturenInfo.Rows(row.Index).Cells(2).Valu e)
e.Graphics.DrawString(strRef3, MyFont, Borstel, 15, IntPrintHo)

'Printing text
strArtikel4 = CStr(dgvFacturenInfo.Rows(row.Index).Cells(3).Valu e)
e.Graphics.DrawString(strArtikel4, MyFont, Borstel, 40, IntPrintHo)

Dim recf5 As New RectangleF(10, IntPrintHo, 133, IntPrintHo)
Dim hh5 As New StringFormat
hh5.Alignment = StringAlignment.Far

'Printing Quantity
intAantal5 = CInt(CStr(dgvFacturenInfo.Rows(row.Index).Cells(4) .Value))
e.Graphics.DrawString(CStr(intAantal5), MyFont, Borstel, recf5, hh5)

'Printing Price
dPrijs7 = CType(((dgvFacturenInfo.Rows(row.Index).Cells(9).V alue)), Decimal)
Dim recf7 As New RectangleF(10, IntPrintHo, 155, IntPrintHo)
Dim hh7 As New StringFormat
hh7.Alignment = StringAlignment.Far
dPrijs7 = CType(((dgvFacturenInfo.Rows(row.Index).Cells(9).V alue)), Decimal)
e.Graphics.DrawString(CStr(CDec(Format(dPrijs7, "#,###0.00"))), MyFont,
Borstel, recf7, hh7)

'Printing Profit
dKorting8 = CType(((dgvFacturenInfo.Rows(row.Index).Cells(10). Value)),
Decimal)
e.Graphics.DrawString(CStr(Format(dKorting8, "0.00")), MyFont, Borstel, 170,
IntPrintHo)

'Printing Totals
Dim recf10 As New RectangleF(10, IntPrintHo, 190, IntPrintHo)
Dim hh10 As New StringFormat
hh10.Alignment = StringAlignment.Far
dTotaal10 = CDec(((dgvFacturenInfo.Rows(row.Index).Cells(13).V alue)))
e.Graphics.DrawString(Format(dTotaal10, "#,###0.00").ToString, MyFont,
Borstel, recf10, hh10)
IntPrintHo = IntPrintHo + 5
intRowPos = intRowPos + 1

'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! This makes the wrong outprint
If intRowPos <= 10 Then
e.HasMorePages = False
Else
e.HasMorePages = True
End If
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Next

End Sub

End Class

والله أعلي وأعلم
RolleyesRolleyesRolleyes
{‏‏ يَرْفَعِ اللَّهُ الَّذِينَ آمَنُوا مِنكُمْ وَالَّذِينَ أُوتُوا الْعِلْمَ دَرَجَاتٍ‏ }

 " Mohamed M. Bedair     -     " Abu Anas
Genius Live , Egypt
الرد }}}
تم الشكر بواسطة: Sajad



التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم