Link to home
Start Free TrialLog in
Avatar of Sok Dymang
Sok DymangFlag for Cambodia

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(dsRptPurchase, "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.Tables(0).SetDataSource(ds)

        End If
        With frmView
            .CrystalReportViewer1.ReportSource = crReportDoc
            crReportDoc.PrintOptions.PrinterName = "Adobe PDF"
            crReportDoc.PrintToPrinter(1, False, 0, 0)
            crReportDoc.Close()
            crReportDoc.Dispose()

        End With
        Return True
    End Function


Thanks in advance.
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

You need to save your user's choice (the last one) before you go into your loop.

I tried many different things to do this, but the only routine that works consistently is this:

      
Public Sub SaveDataGrid(dg As DataGridView)
		Try
			'/ move the focus off the current cell, then back on, to force commit
			dg.NotifyCurrentCellDirty(True)
			Dim OrigCellAddress As Point = New Point(dg.CurrentCellAddress.X, dg.CurrentCellAddress.Y)
			dg.CurrentCell = Nothing
			dg.CurrentCell = dg.Rows(OrigCellAddress.Y).Cells(OrigCellAddress.X)
			dg.CommitEdit(DataGridViewDataErrorContexts.Commit)
		Catch ex As Exception
			'logger.Error(ex.ToString, "MagentoSync.basGlobals.SaveDataGrid Error")
			'MessageBox.Show(String.Format("Error in SaveDataGrid: {0}{1}{2}", Environment.NewLine, Environment.NewLine, ex), "Error")
		Finally
		End Try
	End Sub

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(YourDataGridView)
Avatar of Sok Dymang

ASKER

Use this before loop or in loop brother?
@Scott McDaniel (Microsoft Access MVP - EE MVE )
ASKER CERTIFIED SOLUTION
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you so much brother for your response, let me test first.