Sok Dymang
asked on
For loop in DataGridView Row not read all row. (VB.net)
I build vb.net application and connect to SQL Server 2012, and my code is to print all report that user selected (Checkbox in datagridview), let say DataGrideView has 5 Rows and user select 3 row and press Click on print button and the system will print the 3 item that user selected, but my problem is when user select 3, it will print only 2, if select 2 it will print only 1, and if select only 1 row, it will not print. And here is my code.
For Each drow As DataGridViewRow In DataGridView1.Rows
If drow.Cells(0).Value = True Then
Dim frm As New frmViewReports
Dim rpt As New rptInvoiceKHDolla
Dim dsRptPurchase As New dsReport1
Dim daPurchase As SqlDataAdapter
Dim strSql As String
strSql = "select * from v_RptInvoicesB5 where InvoiceNo=" & drow.Cells(1).Value
daPurchase = New SqlDataAdapter(strSql, sqlcon)
daPurchase.Fill(dsRptPurch ase, "v_RptInvoicesB5")
GF_ViewReport2(rpt, dsRptPurchase, "Print Invoices")
drow.Cells(0).Value = False
End If
Next
and here is my function to print that report.
Public Function GF_ViewReport2(ByVal crReportDoc As ReportDocument, Optional ByVal ds As DataSet = Nothing, Optional ByVal strTitle As String = "") As Boolean
Dim frmView As New frmViewReports
If Not IsNothing(ds) Then
crReportDoc.Database.Table s(0).SetDa taSource(d s)
End If
With frmView
.CrystalReportViewer1.Repo rtSource = crReportDoc
crReportDoc.PrintOptions.P rinterName = "Adobe PDF"
crReportDoc.PrintToPrinter (1, False, 0, 0)
crReportDoc.Close()
crReportDoc.Dispose()
End With
Return True
End Function
Thanks in advance.
For Each drow As DataGridViewRow In DataGridView1.Rows
If drow.Cells(0).Value = True Then
Dim frm As New frmViewReports
Dim rpt As New rptInvoiceKHDolla
Dim dsRptPurchase As New dsReport1
Dim daPurchase As SqlDataAdapter
Dim strSql As String
strSql = "select * from v_RptInvoicesB5 where InvoiceNo=" & drow.Cells(1).Value
daPurchase = New SqlDataAdapter(strSql, sqlcon)
daPurchase.Fill(dsRptPurch
GF_ViewReport2(rpt, dsRptPurchase, "Print Invoices")
drow.Cells(0).Value = False
End If
Next
and here is my function to print that report.
Public Function GF_ViewReport2(ByVal crReportDoc As ReportDocument, Optional ByVal ds As DataSet = Nothing, Optional ByVal strTitle As String = "") As Boolean
Dim frmView As New frmViewReports
If Not IsNothing(ds) Then
crReportDoc.Database.Table
End If
With frmView
.CrystalReportViewer1.Repo
crReportDoc.PrintOptions.P
crReportDoc.PrintToPrinter
crReportDoc.Close()
crReportDoc.Dispose()
End With
Return True
End Function
Thanks in advance.
ASKER
Use this before loop or in loop brother?
@Scott McDaniel (Microsoft Access MVP - EE MVE )
@Scott McDaniel (Microsoft Access MVP - EE MVE )
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you so much brother for your response, let me test first.
I tried many different things to do this, but the only routine that works consistently is this:
Open in new window
It's a bit of a kludge, but it's the only way I've found that works reliably.To call it:
SaveDataGrid(YourDataGridV