Reputation: 1
how to print selected column in datagridview in vb.net? coz I want to print the selected rows in vb.net, the rows have true visibility. this is my code in "PrintDocument1_PrintPage",
With DataGridView1
Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
fmt.LineAlignment = StringAlignment.Center
fmt.Trimming = StringTrimming.EllipsisCharacter
Dim y As Single = 190
Do While mRow < .RowCount
Dim row As DataGridViewRow = .Rows(mRow)
Dim x As Single = 50
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)
e.Graphics.DrawString(DataGridView1.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt)
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
-my problem is, it prints all the rows in my datagridview even if it have false visibility.
Upvotes: 0
Views: 4494
Reputation: 2701
You are not checking to see if the row is Visible or not. As you may know, programming languages can interact with non-visible objects, so just because it is not visible does not mean your code will not see it.
You can probably just put a conditional within your Do While loop...
Do WHile mRow < .RowCount
Dim row as DataGridViewRow = .Rows(mRow)
If row.Visible then
'blah blah blah
EndIf
Loop
Upvotes: 1