2012-02-23 64 views
0

我有一個程序,顯示datagridview中的數據。 datagridview中的數據來自數據集中的表格。數據在屏幕上看起來不錯,所以我決定添加一種打印數據的方式。我在我的數據集中有數據,但沒有顯示在我的報告查看器中?

我創建了一個新窗體,添加了一個reportviewer控件,並設計了這個報表。我使用我的數據集作爲報表的數據源。

然後,我將一個按鈕添加到我的原始窗體中,以便當它被按下時,它將顯示帶有reportviewer控件的窗體。

我的問題是,當我點擊打印按鈕,它將我帶到我的reportviewer控件窗體,但它顯示我的報告只有標題,沒有數據。這就像我的數據集中沒有數據!但是,當我逐步完成調試時,它會在我的數據集中顯示超過1000行。

所以,我的問題是,我忘記了什麼?數據在那裏,它顯示在一個表單上(使用datagridview),但它不顯示在reportviewer控件上(只有標題)。

確實沒有涉及的編碼。我只是做了一個新的表單,添加了reportviewer控件,設計了報告並告訴它使用我的數據集作爲數據源。通常,這對我有用。我無法想象爲什麼它不起作用。

感謝您的任何幫助或建議!

下面是我用它來顯示報告代碼:

private void btnPrint_Click(object sender, EventArgs e) 
    { 
     Form showReport = new frmPrintView(); 
     showReport.Show(); 
    } 

這裏是我的兩個屏幕上的圖像。數據顯然在我的數據集中,否則第一個屏幕上就沒有數據。然而,第二個屏幕似乎顯示我的數據集是空的,因爲除了標題外沒有任何內容出現。

enter image description here

enter image description here

回答

2

我認爲你缺少DataBind方法:

從考試70-516:TS:使用Microsoft .NET框架訪問數據4:

ASP.NET controls require you to execute the DataBind method on the control to indicate 
that the data is ready to be rendered. If you don’t execute the DataBind method, the control won’t render. When executing the DataBind method on a control, the control is obliged to call the DataBind method on its child controls. This means that you can execute the DataBind method on the Web form, and it will call the DataBind method on all its controls 

將其添加到窗體報告中的表單加載事件

+0

謝謝,迭戈!我查看了加載方法的代碼,其中包含的所有代碼都是刷新報表的語句,但沒有用於數據綁定。我很感激! – Kevin 2012-02-23 20:35:22

0

視頻支持https://www.youtube.com/watch?v=hkDIDTNbA6M

u需要再次從數據數據庫

加入到烏爾數據集
billDataSet b1 = new billDataSet(); 

SqlDataAdapter s = new SqlDataAdapter("select * from TblOrder",con); 

s.Fill(b1,b1.Tables[0].TableName); 

ReportDataSource rds = new ReportDataSource("orders",b1.Tables[0]); 

this.reportViewer1.LocalReport.DataSources.Clear(); 

this.reportViewer1.LocalReport.DataSources.Add(rds); 

this.reportView er1.LocalReport.Refresh(); 

this.TblOrderTableAdapter.Fill(this.billDataSet.TblOrder, d1.ToString(),d2.ToString(), companyid); 

this.reportViewer1.RefreshReport(); 
相關問題