VS2015 C# RDLC自定义数据报表
使用VS2015自带的数据库开发工具开发自定义报表,说是自定义,其实也没啥自定义的。只不过是刚好把需要用的信息传进去而已。
说说我的理解:
一个报表项目需要用的最少需要三个组件:
1:展示页面。ReportViewer,
2:数据集,dataSet.xsd,
3:一个报表模板,report.rdlc
第一个组件(reportviewer)很好理解,就是一个在页面上展示的底板。
第二个组件dataset.xsd,这个组件是保存数据的组件,比如查询数据库得到的数据集,都要装在这个组件中,然后再在这个组件中传入报表模板,报表模板经过数据转换,将绑定到reportview中额数据集展示出来。
1:新建一个项目,项目选择 报表,我使用的是默认命名。
2:新建一个数据集
3:配置数据集。
在数据集设计窗口中右键—添加—datatable
4:为数据集新建列数据。
这里增加的列是以后要显示在报表上的列,这里我填了emp,po,dwgno,quantity。。
5:然后在rdlc模板中选择表,。其实这里可以选的展示方式有很多种,只是看需求。
选择表之后会出来一个数据源的选择,选择dataset1。
然后添加相应的字段,这里的字段只要选择在dataset1 中的字段就好了。
6:在winfrom页面中,添加一个报表控件reportviewer,在选择报表,选择要展示的数据原报表。
到这里拖控件的步骤基本完成,接着要在展示页面中写一些代码了;
form1代码如下:`using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load_1(object sender, EventArgs e)
{
string str = "select * from xiaolv"; //查询数据库语句
DataSet ds = new dbhelp().Query(str); //将数据装到dataset中。dbhelp()是一个我自己写的数据库链接类。
DataTable dt = new DataTable(); //新建一个要装载数据的模板
dt.Columns.Add(new DataColumn("emp", typeof(string)));
dt.Columns.Add(new DataColumn("po", typeof(string)));
dt.Columns.Add(new DataColumn("dwgno", typeof(string)));
dt.Columns.Add(new DataColumn("quantity", typeof(string)));
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
DataRow dr = dt.NewRow();
dr[0] = ds.Tables[0].Rows[i]["Employee"].ToString();
dr[1] = ds.Tables[0].Rows[i]["po"].ToString();
dr[2] = ds.Tables[0].Rows[i]["dwgno"].ToString();
dr[3] = ds.Tables[0].Rows[i]["goodqty"].ToString();
dt.Rows.Add(dr);
}
this.reportViewer2.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local;
this.reportViewer2.LocalReport.DataSources.Clear();
this.reportViewer2.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", dt));
this.reportViewer2.RefreshReport();
this.reportViewer2.RefreshReport();
}
}
}
`
运行之后的效果图: