步骤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();