Ok so this might be an obvious one but it got me stuck for a little while so thought I should post it. When you use the PrintPreviewDialog the documents Print method is automatically called when the dialog is shown. But using the PrintDialog the Print method of the associated PrintDocument is not called when the user clicks the Print button on the dialog, you have to check that OK was returned as the dialogs result!
There are a few gotchas with checking the value of a CheckBox in a DataGridView. You can use the CellValueChanged event, the bound object will have been updated so its easy to check…
Private Sub PaymentsDataGridView_CellValueChanged(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
Handles PaymentsDataGridView.CellValueChanged
'This code will be called while loading the grid as well so I
'just check for it by setting a flag
If Not _Loading AndAlso e.ColumnIndex = 4 AndAlso e.RowIndex >= 0 Then
Dim aSummary As RebatePatientSummary = _
DirectCast(PaymentsDataGridView.Rows(e.RowIndex).DataBoundItem, _
RebatePatientSummary)
If aSummary.PrintMe Then
_PrintCount += 1
Else
_PrintCount -= 1
End If
AvailableToPrintLabel.Text = "Selected for printing : " & _PrintCount
End If
End Sub
That gotcha here though is that the event is only fired once the cell has lost focus!
You can use the CurrentCellDirtyStateChanged but that only fires once so if the user clicks the checkbox multiple times you don’t actually know the final state
If you are generating multiple pages in a document you set the e.HasMorePages to true, normally this is done by checking how many pages you have to print, e.g.
If the user then decides to Print the pages they are likely to only see the first page because the _CurrentPageNumber has not been reset to zero, as the Print command actually regenerates the pages by firing your code again not using what it has already created.
The tip here is to reset the controlling counter in the BeginPrint event