dcsimg
Results 1 to 5 of 5

Thread: ReportViewer and Form Binding Source

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2016
    Posts
    45

    ReportViewer and Form Binding Source

    I hope I'm explaining this correctly. I'm using VB2019, ReportViewer, and Access Database.

    I have a form with a datagridview that is bound via code to a table, let's say tblJobs in my database, PWRBase. I have a button on the form to produce a reporting using ReportViewer.

    Can the report use the same datasource/bindingsource/table adapters, etc that the datagridview on the form is using? Sometimes when the form is displayed, I'll pass a different adhoc query to it, so I would like the report results to match the datagridview. I've tried searching, but haven't found an answer. Thanks

  2. #2

    Thread Starter
    Member
    Join Date
    Jul 2016
    Posts
    45

    Re: ReportViewer and Form Binding Source

    Datagridview form code.

    Code:
    Public Class FrmJobGrid
        Inherits Form
        Private WithEvents dgvJobs As New DataGridView()
        Private bindingSource1 As New BindingSource()
        Private jobsDataAdapter As New OleDbDataAdapter()
        Private WithEvents btnReload As New Button()
        Private WithEvents btnSelect As New Button()
        Private WithEvents btnSubmit As New Button()
        Private WithEvents btnReport As New Button()
        Private WithEvents btnExit As New Button()
        Public Sub New()
            dgvJobs.Dock = DockStyle.Fill
            btnReload.Text = "Reload"
            btnSubmit.Text = "Submit"
            btnSelect.Text = "Select"
            btnReport.Text = "Report"
            btnExit.Text = "OK"
            Dim panel As New FlowLayoutPanel With {
                    .Dock = DockStyle.Top,
                    .AutoSize = True            }
            panel.Controls.AddRange(New Control() {btnReload, btnSelect, btnSubmit, btnReport, btnExit})
            Controls.AddRange(New Control() {dgvJobs, panel})
         End Sub
        Private Sub GetData(ByVal selectCommand As String)
            Try
                     ConnectDatabase()
                  jobsDataAdapter = New OleDbDataAdapter(selectCommand, PWRConn)
                Dim jobsCommandBuilder As New OleDbCommandBuilder(jobsDataAdapter)
             Dim table As New DataTable With {
                    .Locale = Globalization.CultureInfo.InvariantCulture            }
                jobsDataAdapter.Fill(table)
                bindingSource1.DataSource = table
                DisconnectDatabase()
            Catch ex As OleDbException
                MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK)
                End
            End Try
        End Sub
        Private Sub FrmJobGrid_Load(ByVal sender As Object, ByVal e As EventArgs)  Handles Me.Load
            If adoJobs.Rows.Count = 0 Then
                FormReload("All")
            End If
        End Sub
        Public Sub FormReload(ByVal x As String)
            Dim sqlstring As String
            dgvJobs.DataSource = bindingSource1
            Select Case x
                Case "All"
                    GetData("SELECT * FROM tblJobs")
                Case "Fix"
                    GetData("SELECT * FROM tblJobs WHERE (((tblJobs.[DefaultTrade])=' '));")
            End Select
        End Sub
        Private Sub btnReport_click(ByVal sender As Object, ByVal e As EventArgs) Handles btnReport.Click
            Dim Report As New FrmReport
            With Report
                .strReport = "Jobs Listing"
                .strType = "All"
                .Show()
            End With
        End Sub
    End Class

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2016
    Posts
    45

    Re: ReportViewer and Form Binding Source

    Reportviewer form code:

    Code:
    Public Class FrmReport
        Public Property strReport As String
        Public Property strType As String
        Private Sub frmReport_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim rptDataSource As ReportDataSource
            Dim sqlAll As String
            Dim sqlInactive As String
            Dim sqlActive As String
            Dim sql As String = ""
            Try
                With Me.ReportViewer1.LocalReport
                    .ReportEmbeddedResource = "Prevailing_Wage." & strReport & ".rdlc"
                    .DataSources.Clear()
                End With
                Select Case strReport
                    Case "Jobs Listing"
                        sqlAll = "SELECT * FROM tblJobs"
                        sqlInactive = "SELECT * FROM tblJobs WHERE Inactive=true"
                        sqlActive = "SELECT * FROM tblJobs WHERE Inactive=false"
                        Select Case strType
                            Case "All"
                                sql = sqlAll
                            Case "Active"
                                sql = sqlActive
                            Case "Inactive"
                                sql = sqlInactive
                        End Select
                        Dim ds As New PWRBaseDataSet
                        Dim da As New Prevailing_Wage.PWRBaseDataSetTableAdapters.tblJobsTableAdapter
                        '  da.Adapter.SelectCommand.CommandText = sql   'DOES NOT WORK
                        da.Fill(ds.tblJobs)
                        rptDataSource = New ReportDataSource("dsJobs", ds.Tables("tblJobs"))
                End Select
                Me.ReportViewer1.LocalReport.DataSources.Add(rptDataSource)
                Me.ReportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout)
                Me.ReportViewer1.RefreshReport()
            Catch ex As Exception
                MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
    End Class

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,444

    Re: ReportViewer and Form Binding Source

    You should certainly be able to use the same data for the report as you used for the grid. Try using the BindingSource first, which I think should work. If it doesn't, you should certainly be able to use the same DataTable as is bound to the BindingSource.

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2016
    Posts
    45

    Re: ReportViewer and Form Binding Source

    Thanks for your help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width