Kod:
Public Class Form1
Dim strFormat As StringFormat
Dim arrColumnLefts As New ArrayList()
Dim arrColumnWidths As New ArrayList()
Dim iCellHeight As Integer = 0
Dim iTotalWidth As Integer = 0
Dim iRow As Integer = 0
Dim bFirstPage As Boolean = False
Dim bNewPage As Boolean = False
Dim iHeaderHeight As Integer = 0
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Try
Dim iLeftMargin As Integer = e.MarginBounds.Left
Dim iTopMargin As Integer = e.MarginBounds.Top
Dim bMorePagesToPrint As Boolean = False
Dim iTmpWidth As Integer = 0
bFirstPage = True
If bFirstPage Then
For Each GridCol As DataGridViewColumn In DataGridView1.Columns
iTmpWidth = CInt(Math.Floor(CDbl(CDbl(GridCol.Width) / CDbl(iTotalWidth) * CDbl(iTotalWidth) * (CDbl(e.MarginBounds.Width) / CDbl(iTotalWidth)))))
iHeaderHeight = CInt(e.Graphics.MeasureString(GridCol.HeaderText, GridCol.InheritedStyle.Font, iTmpWidth).Height) + 11
arrColumnLefts.Add(iLeftMargin)
arrColumnWidths.Add(iTmpWidth)
iLeftMargin += iTmpWidth
Next
End If
While iRow <= DataGridView1.Rows.Count - 1
Dim GridRow As DataGridViewRow = DataGridView1.Rows(iRow)
iCellHeight = GridRow.Height + 5
Dim iCount As Integer = 0
If iTopMargin + iCellHeight >= e.MarginBounds.Height + e.MarginBounds.Top Then
bNewPage = True
bFirstPage = False
bMorePagesToPrint = True
Exit While
Else
If bNewPage Then
e.Graphics.DrawString("Çıktı Başlığı", New Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - e.Graphics.MeasureString("Çıktı Başlığı", New Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13)
Dim strDate As [String] = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString()
e.Graphics.DrawString(strDate, New Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(strDate, New Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Width), e.MarginBounds.Top - e.Graphics.MeasureString("Çıktı Başlığı", New Font(New Font(DataGridView1.Font, FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13)
iTopMargin = e.MarginBounds.Top
For Each GridCol As DataGridViewColumn In DataGridView1.Columns
e.Graphics.FillRectangle(New SolidBrush(Color.LightGray), New Rectangle(CInt(arrColumnLefts(iCount)), iTopMargin, CInt(arrColumnWidths(iCount)), iHeaderHeight))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(CInt(arrColumnLefts(iCount)), iTopMargin, CInt(arrColumnWidths(iCount)), iHeaderHeight))
e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font, New SolidBrush(GridCol.InheritedStyle.ForeColor), New RectangleF(CInt(arrColumnLefts(iCount)), iTopMargin, CInt(arrColumnWidths(iCount)), iHeaderHeight), strFormat)
iCount += 1
Next
bNewPage = False
iTopMargin += iHeaderHeight
End If
iCount = 0
For Each Cel As DataGridViewCell In GridRow.Cells
If Cel.Value IsNot Nothing Then
e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font, New SolidBrush(Cel.InheritedStyle.ForeColor), New RectangleF(CInt(arrColumnLefts(iCount)), CSng(iTopMargin), CInt(arrColumnWidths(iCount)), CSng(iCellHeight)), strFormat)
End If
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(CInt(arrColumnLefts(iCount)), iTopMargin, CInt(arrColumnWidths(iCount)), iCellHeight))
iCount += 1
Next
End If
iRow += 1
iTopMargin += iCellHeight
End While
If bMorePagesToPrint Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
Catch exc As Exception
MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
End Try
End Sub
Private Sub PrintDocument1_BeginPrint(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
Try
strFormat = New StringFormat()
strFormat.Alignment = StringAlignment.Near
strFormat.LineAlignment = StringAlignment.Center
strFormat.Trimming = StringTrimming.EllipsisCharacter
arrColumnLefts.Clear()
arrColumnWidths.Clear()
iCellHeight = 0
iRow = 0
bFirstPage = True
bNewPage = True
iTotalWidth = 0
For Each dgvGridCol As DataGridViewColumn In DataGridView1.Columns
iTotalWidth += dgvGridCol.Width
Next
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
End Try
End Sub
Kod:
Dim GayriSafiToplam As Double
Dim index As Integer
For index = 0 To Me.MustahsilListesiDataGridView.RowCount - 1
If MustahsilListesiDataGridView.RowCount > 1 Then
Try
'iTax += Convert.ToInt32(Me.MustahsilListesiDataGridView.Rows(index).Cells(11).Value)
GayriSafiToplam += FormatNumber(Me.MustahsilListesiDataGridView.Rows(index).Cells(9).Value, 2, TriState.True)
Catch ex As Exception
Me.MustahsilListesiDataGridView.Rows(index).DefaultCellStyle.BackColor = Color.Red
End Try
End If
Next
Label1.Text = GayriSafiToplam