我們常常用reportViewer來做一些報表,直接連數據源的不講了,google上有,這里是動態綁定一些字段和數據源,
添加
這兩個dll,工具箱會直接有這個組件
拖拽一個
form的代碼
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.reportViewer1.ProcessingMode = ProcessingMode.Local; this.reportViewer1.LocalReport.ReportPath = "TestReport.rdlc"; this.reportViewer1.LocalReport.EnableExternalImages = true; List<ReportParameter> para = new List<ReportParameter>(); //這里是添加兩個字段 para.Add(new ReportParameter("FishName", "fishkel")); para.Add(new ReportParameter("FishId", "123")); //這里是添加兩個數據源,兩個list var list = new List<TestReport> { }; list.Add(new TestReport() { a = "20100201", b = 0.1, c = 0.2, d = 0.1 }); list.Add(new TestReport() { a = "20100202", b = 0.1, c = 0.2, d = 0.2 }); list.Add(new TestReport() { a = "20100203", b = 0.1, c = 0.4, d = 0.2 }); var test = new List<TestReport>() {new TestReport(){ a = "20100201", b = 0.33, c = 0.33, d = 0.33 }}; this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("Test", test)); this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("TestList", list)); this.reportViewer1.LocalReport.SetParameters(para); this.reportViewer1.RefreshReport(); } } public class TestReport { public string a { get; set; } public double b { get; set; } public double c { get; set; } public double d { get; set; } } }
新建一個報表。rdlc類型的,打開報表,拖拽一個圖表類型
因為你沒有添加任何數據源,然后它會彈出
名稱你自己起,數據源選擇新建,選擇對象,然后選擇你自己新建的那個類,就是我建的TestReport那個,點擊完成
我添加了兩個數據源和兩個參數,因為form1中我傳來了兩個數據源兩個參數,這里必須要一一對應,不然會找不到數據,添加完了,
右鍵 圖表屬性。選擇一個數據源,然后把字段添加到區域
拖拽一個文本框控件,右鍵 表達式
雙擊,確定,好了,form中的那個報表 this.reportViewer1.LocalReport.ReportPath = "TestReport.rdlc"; 這個 是要在bin debug有副本的,也就是說你要把報表的 復制到輸出路徑改成始終復制,然后把form的報表控件右上角的 選擇報表 選擇一下,運行:
ok 剩下的自己去擺放吧! goodluck!