標籤:
步驟1.建立一個視窗,拖一個ReportViewer控制項上去,設定該控制項的Dock為Fill。
步驟2.添加一個報表檔案,如:Report1.rdlc,設定其中的參數,這裡設定的參數和下面的代碼裡面的參數是對應的。
1 //載入報表檔案,Debug目錄下 2 this.reportViewer1.LocalReport.ReportPath = "Report1.rdlc"; 3 4 this.reportViewer1.ProcessingMode = ProcessingMode.Local; 5 reportViewer1.LocalReport.DataSources.Clear(); 6 DataTable dt = new DataTable(); 7 DataColumn col1 = new DataColumn("姓名", System.Type.GetType("System.String")); 8 DataColumn col2 = new DataColumn("學號", System.Type.GetType("System.String")); 9 DataColumn col3 = new DataColumn("性別", System.Type.GetType("System.String"));10 DataColumn col4 = new DataColumn("年齡", System.Type.GetType("System.Int32"));11 dt.Columns.Add(col1);12 dt.Columns.Add(col2);13 dt.Columns.Add(col3);14 dt.Columns.Add(col4);15 DataRow row1 = dt.NewRow();16 row1["姓名"] = "張三";17 row1["學號"] = "2016111";18 row1["性別"] = "男";19 row1["年齡"] = 20;20 dt.Rows.Add(row1); 21 //待用資料不用加資料來源22 //ReportDataSource rds1 = new ReportDataSource("DataSet1", dt);23 //reportViewer1.LocalReport.DataSources.Add(rds1);24 ReportParameter ReportParameter_XM = new ReportParameter("ReportParameter_XM", dt.Rows[0]["姓名"].ToString());25 ReportParameter ReportParameter_XH = new ReportParameter("ReportParameter_XH", dt.Rows[0]["學號"].ToString());26 ReportParameter ReportParameter_XB = new ReportParameter("ReportParameter_XB", dt.Rows[0]["性別"].ToString());27 ReportParameter ReportParameter_NL = new ReportParameter("ReportParameter_NL", dt.Rows[0]["年齡"].ToString());28 reportViewer1.LocalReport.SetParameters(new ReportParameter[] { ReportParameter_XM, ReportParameter_XH, ReportParameter_XB, ReportParameter_NL });29 reportViewer1.RefreshReport();
C#winform初試報表