I fill a DatagridView with data, then I want to print this data in alternating colors, but when I do my PrintPreview I only see my columns show alternating colors.
Private Sub PrintDocument1_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
With DataGridView1
Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
fmt.LineAlignment = StringAlignment.Center
fmt.Trimming = StringTrimming.EllipsisCharacter
Dim y As Single = e.MarginBounds.Top - 50
Dim myBrush As Brush
Dim myBrush1 As Brush
Dim rowID As Integer = 0
myBrush = New SolidBrush(Color.AliceBlue)
myBrush1 = New SolidBrush(Color.White)
Do While mRow < .RowCount
Dim row As DataGridViewRow = .Rows(mRow)
Dim x As Single = e.MarginBounds.Left - 65
Dim h As Single = 0
For Each cell As DataGridViewCell In row.Cells
Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height)
e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height)
If (newpage) Then
'e.Graphics.FillRectangle(myBrush, rc)
e.Graphics.DrawString(DataGridView1.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, fmt)
Else
If rowID = 0 Then
e.Graphics.FillRectangle(myBrush, rc)
rowID = 1
ElseIf rowID = 1 Then
e.Graphics.FillRectangle(myBrush1, rc)
rowID = 0
End If
e.Graphics.DrawString(DataGridView1.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt)
End If
x += rc.Width
h = Math.Max(h, rc.Height)
Next
newpage = False
y += h
mRow += 1
If y + h > e.MarginBounds.Bottom Then
e.HasMorePages = True
mRow -= 1
newpage = True
Exit Sub
End If
Loop
mRow = 0
End With
End Sub
I have tried to place the
If rowID = 0 Then
e.Graphics.FillRectangle(myBrush, rc)
rowID = 1
ElseIf rowID = 1 Then
e.Graphics.FillRectangle(myBrush1, rc)
rowID = 0
End If
in other places, but I get the same results. This use to be easy in ASP.Net, but now I am working in VB.Net. Any ideas?
The issue is that your brush selection is inside the For Each
loop over the cells. If you don't want to change the brush on each cell then don't select the brush on each cell. Put the code that selects the brush inside the outer loop but outside the inner loop.
You should be doing something like this:
Dim useAlternateBrush = False
Using standardBrush As New SolidBrush(Color.AliceBlue),
alternateBrush As New SolidBrush(Color.White)
Do While mRow < .RowCount
'...
Dim brush = If(useAlternateBrush, alternateBrush, standardBrush)
useAlternateBrush = Not useAlternateBrush
'...
For Each cell As DataGridViewCell In row.Cells
'...
'Use brush here.
'...
Next
Loop
End Using
Notice that the SolidBrush
objects are created with a Using
statement, so that they are implicitly disposed at the End Using
statement. Disposable objects should ALWAYS be disposed if at all possible, which is almost always the case, and short-lived, disposable objects should be created and disposed with a Using
block.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.