2012-08-17 90 views
1

我在vb.net中有一个reportviewer,我有2个.rdlc文件,即Report1和Report2。 它们具有与表格相同的设计,但Report1具有参数和过滤器,Report2只显示我的记录中的所有内容。在运行时绑定/重新绑定datasource到reportviewer vb.net

我知道如何将数据源绑定到reportviewer在设计时,但我不知道如何去做它运行时,我需要切换数据源,当表单加载第一次,当用户实际上搜索的东西。基本上这是我的想法。

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

我需要显示所有记录的第一次加载窗体。所以我将需要Report2.rdlc为没有过滤器。

当我有Report1.rdlc绑定,这就是我们看到的

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

没有显示除了我们在文本框中输入一些值,然后单击搜索,记录将根据加载关于我们正在寻找的东西。

这里是代码。

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 

回答

2

用过滤的数据填充您的数据表。然后绑定它。

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

喜欢的东西:

Table1 = FilteredQueryAsDataTable 

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

此代码的帮助ü:)(vb.net 2008码) 我使用这个代码的按钮。所以当我按下它时水晶报告将显示在水晶报告浏览器中并显示数据集中的数据。

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