locked
How to filter a report based on bindingsource.filter? RRS feed

  • Question

  • I have searched the forum for an answer to my problem, but I can't seem to find anything related to my specific problem.  I have a report that lists, among other things, a person's name.  The data used for the report comes from a dataset called DataDataSet.Player_Info.  I have created a report that lists the information for every player.  I have set the DataSetName of the report to DataDataSet_Player_Info.  That works fine.  What I want to do however, is have the user select a name(s) from a listbox on another form prior to opening the report to filter the report to just show the information for that specific player(s).  I have created a form called TL_Player_Filter.  Here is the code that comes up with a string to filter a bindingsource I have on the form with the report:

    Function constructString() As String
         Dim strFilterString As String = ""
         If ListBox1.SelectedItems.Count > 0 Then
              For Each SelectedName As Integer In Me.ListBox1.SelectedIndices 
                   If strFilterString.Length > 0 Then strFilterString = strFilterString & " OR "
                   strFilterString = strFilterString & "Player='" & Me.DataDataSet.Main.Rows(SelectedName).Item("Player") & "'"
              Next
         End If
    Return strFilterString
    End Function

    On the form with the report I have a bindingsource.filter to filter the data used for the report:

    Me.Player_InfoBindingSource.Filter = TL_Player_Filter.constructString()

    I have tried to make the bindingsource the data used for the report based on other posts to this forum, with no success.  I've even tried to set a parameter or filter within the report under the Filter tab of the table in the report.  Again, I either get no data passed to the report, or I get all the records in the report even though I select a specific name from the listbox.  I know the constructString() works because I can use it to filter data on another form.  I just can't get it to pass the data to the report.

    Any help would be greatly appreciated.  Please let me know if you need further detail of what I'm trying to do.  Also, I'm programming in VB, not C+ and my programming knowledge is somewhat limited (self-taught).

    Thanks,
    John 
     
    • Edited by reichard2 Friday, October 9, 2009 4:43 AM
    Sunday, October 4, 2009 7:27 PM

Answers

  • I believe I have found the solution to my problem.  And as is the case with most of the problems I have, the solution was staring me right in the face, but I didn't see it.  Using the following post as a starting point (http://social.msdn.microsoft.com/Forums/en-US/vsreportcontrols/thread/8abbf26b-79b0-43ff-b49b-8a6348ae6741) and incorporating some advice from this post (http://social.msdn.microsoft.com/Forums/en-US/vsreportcontrols/thread/f2216f66-1f20-49f1-a18a-a871f87d8d53) I used the following code:
     
    Dim_filteredTable As DataTable
    _filteredTable =
    DirectCast(Me.Player_InfoBindingSource.Current, DataRowView).DataView.ToTable()
    Dim reportDataSource1 As New Microsoft.Reporting.WinForms.ReportDataSource()
    reportDataSource1.Value = _filteredTable
    Me.ReportViewer1.LocalReport.DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource("DataDataSet_Player_Info", _filteredTable))

    Initially, even this didn't seem to filter the data as the report would show everything.  Then I opened the form that contained the ReportViewer1 and noticed that in the dropdown menu at the top right where you select the report to use, I had the following selected for 'Choose Data Sources':

    Report Data Source = DataDataSet_Player_Info
    Data Source Instance = Player_InfoBindingSource

    I decided to remove Player_InfoBindingSource from the Data Source Instance so '(none)' was selected and PRESTO!  The above code I used kicks in and uses the filtered BindingSource for the data of the report and it works perfectly!

    Hopefully this will help others out there.

    John
    • Marked as answer by reichard2 Friday, October 9, 2009 4:40 AM
    Friday, October 9, 2009 4:38 AM