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);
ReportParameter fromParam= new ReportParameter("FromDate",dateTimePicker1.Value.ToShortDateString());
ReportParameter toParam = new ReportParameter("ToDate", dateTimePicker1.Value.ToShortDateString());
reportViewer1.LocalReport.SetParameters(new ReportParameter[]{fromParam,toParam});
reportViewer1.RefreshReport();
}
private DataTable getProdFinishTable()
{
SqlConnection connection= new SqlConnection(Properties.Settings.Default.MESDBConnectionString);
connection.Open();
string sql = "select * from prodfinish where proddate>=@fromdate and proddate<dateadd(day,1,@todate)";
SqlDataAdapter adapter= new SqlDataAdapter(sql,connection);
adapter.SelectCommand.Parameters.AddWithValue("fromdate",dateTimePicker1.Value.Date);
adapter.SelectCommand.Parameters.AddWithValue("todate", dateTimePicker2.Value.Date);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
connection.Close();
return dataTable;
}