from:http://www.cnblogs.com/duanshuiliu/archive/2012/07/13/2589862.html
使用ReportView控件可以显示远端Report service的报表,也可以处理生成本地报表,用法也比较简单,下面列举一下简单的步骤。
首先使用Report Wizard添加一个rdlc报表文件到工程项目,使用这个wizard会自动创建报表使用的DataSet等,其实这些后面都可以用自己的DataSource来替换掉。设计好一个Table的报表,在报表中添加FromDate和ToDate两个参数,然后添加两个TextBox在报表上显示这两个参数的值。
然后添加一个ReportView控件到Form,在一个按钮事件中来初始化和刷新报表:
1 private void button1_Click(object sender, EventArgs e)
2 { 3 reportViewer1.LocalReport.ReportEmbeddedResource = " ReportsApplication1.Report1.rdlc "; 4 5 ReportDataSource reportDataSource= new ReportDataSource(); 6 reportDataSource.Name = " MESDBDataSet_ProdFinish "; 7 reportDataSource.Value = getProdFinishTable(); 8 reportViewer1.LocalReport.DataSources.Clear(); 9 reportViewer1.LocalReport.DataSources.Add(reportDataSource); 10 11 ReportParameter fromParam= new ReportParameter( " FromDate ",dateTimePicker1.Value.ToShortDateString()); 12 ReportParameter toParam = new ReportParameter( " ToDate ", dateTimePicker1.Value.ToShortDateString()); 13 14 reportViewer1.LocalReport.SetParameters( new ReportParameter[]{fromParam,toParam}); 15 reportViewer1.RefreshReport(); 16 } 17 18 private DataTable getProdFinishTable() 19 { 20 21 SqlConnection connection= new SqlConnection(Properties.Settings.Default.MESDBConnectionString); 22 connection.Open(); 23 string sql = " select * from prodfinish where proddate>=@fromdate and proddate<dateadd(day,1,@todate) "; 24 SqlDataAdapter adapter= new SqlDataAdapter(sql,connection); 25 adapter.SelectCommand.Parameters.AddWithValue( " fromdate ",dateTimePicker1.Value.Date); 26 adapter.SelectCommand.Parameters.AddWithValue( " todate ", dateTimePicker2.Value.Date); 27 DataTable dataTable = new DataTable(); 28 adapter.Fill(dataTable); 29 connection.Close(); 30 return dataTable; 31 }