2012-08-17 91 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