user1566093
user1566093

Reputation: 11

binding/rebinding datasource to reportviewer vb.net at runtime

i have a reportviewer in vb.net and i have 2 .rdlc files namely Report1 and Report2. they have both the same design as a table but Report1 has parameter and filter, Report2 just displays everything from my record.

i know how to bind datasources to reportviewer at design time but i don't know how to do it run time, i need switching of datasources when the form loads the first time and when the user actually search something. basically this is what i have in mind.

http://imageshack.us/photo/my-images/407/reportzm.png/

i need all the records to be displayed the first time the form loads. so i will be needing Report2.rdlc for that which has no filters.

When i have Report1.rdlc binded, this is what we see

http://imageshack.us/photo/my-images/255/er11.png/

nothing is displayed except we enter some value in the textbox and click search, records will be loaded based on what we are searching for.

here is the code.

Imports Microsoft.Reporting.WinForms

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'houseDataSet.Table1' table. You can move, or remove it, as needed.


        Me.Table1TableAdapter.Fill(Me.houseDataSet.Table1)

        Me.ReportViewer1.RefreshReport()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a As New ReportParameter("ReportParameter1", TextBox1.Text)
        ReportViewer1.LocalReport.SetParameters(New ReportParameter() {a})
        ReportViewer1.RefreshReport()
    End Sub
End Class

Upvotes: 1

Views: 11387

Answers (2)

Mohammad
Mohammad

Reputation: 1

this code might help u :) (vb.net 2008 code) i'm using this code in a button. so when i press it; the crystal report will be shown on the crystal report viewer and show the data from the dataset.

    Dim rpt As New CrystalReport1() 'The report you created. 
    Dim myConnection As SqlConnection
    Dim MyCommand As New SqlCommand()
    Dim myDA As New SqlDataAdapter()
    Dim myDS As New Database1DataSet1() 'The DataSet you created. 
    Try
        myConnection = New SqlConnection("type here your connection string")
        MyCommand.Connection = myConnection
        MyCommand.CommandText = "Select  * from table"
        MyCommand.CommandType = CommandType.Text
        myDA.SelectCommand = MyCommand
        myDA.Fill(myDS, "type here table name")
        rpt.SetDataSource(myDS)
        rptViewer.ReportSource = rpt
    Catch Excep As Exception
        MessageBox.Show(Excep.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try

Upvotes: 0

oiksam
oiksam

Reputation: 56

fill your data table with the filtered data. and then bind it.

Me.Table1TableAdapter.Fill(Me.houseDataSet.Table1)

to something like:

Table1 = FilteredQueryAsDataTable

Me.Table1TableAdapter.Fill(Me.houseDataSet.Table1)

Upvotes: 2

Related Questions