1. 程式人生 > >VS2010自帶的報表(reportviewer)使用方法

VS2010自帶的報表(reportviewer)使用方法

本來想做一個水晶報表,突然發現VS2010沒有自帶的水晶報表,還要安裝相應的外掛。於是學了一下VS2010自帶的報表。據說比水晶報表的載入速度快。下面詳細介紹一下使用步驟:

1.先建立一個本地的資料庫,右鍵單擊你的專案-->選擇【Add】--->New Item--->Local database.建立資料庫後,新增一個數據表T_student,新增一些資料。

2.右鍵---->【Add】--->New Item--->Dataset(命名為information.xsd),把剛才建立的表T_student直接拖到information.xsd的設計介面上。

3.右鍵---->【Add】---->New Item---> Report(命名為report.rdlc),在report.rdlc的介面上右鍵---->【insert】---->【table】,此時會出現一個配置視窗,第一個【Name】填寫你新增的dataset的名稱(information),

Data source選項選擇information.

4.然後到winform介面。新增ReportViewer控制元件

5.在Form.cs中編寫程式碼:

 private void button2_Click(object sender, EventArgs e)
        {
            information ds1 = new information();
            informationTableAdapters.table11TableAdapter ap = new informationTableAdapters.table11TableAdapter();
            ap.Fill(ds1.table11);
            DataTable dt1 = new DataTable();
            dt1 = ds1.table11;

            this.reportViewer1.Reset();
            this.reportViewer1.LocalReport.DataSources.Clear();
            this.reportViewer1.LocalReport.ReportPath = @"E:\test code\水晶報表\水晶報表\report1.rdlc";
            ReportDataSource rds = new ReportDataSource("information", dt1); //ReportDataSource資料來源的第一個引數必須與你新增的dataset的名字相同

            this.reportViewer1.LocalReport.DataSources.Add(rds);  //新增資料來源
this.reportViewer1.ZoomMode = ZoomMode.Percent;
            this.reportViewer1.RefreshReport();
        }