本博客純屬原創
1、新建windows窗體,項目-->添加新項-->Visual C#項-->Windows Form-->windows窗體。
2、窗體中加入button按鈕和報表控件。
3、新建報表,項目-->添加新項-->reporting-->報表,生成.rdlc文件。
4、在.rdlc上插入表格-->新建數據源-->數據庫-->數據集-->新建連接,步驟如下:
5、這里輸入數據庫連接的服務器名,登錄數據庫的用戶名和密碼,選擇數據庫名稱。
6、選擇需要用的表。
7、名稱可以自己寫,本人沒改,數據源為自己選擇的數據庫名稱,可用數據集為表名。
8、.rdlc界面在表格的第一行輸入字段名,第二行點擊每列右上角的小圖標選擇該列要顯示的字段名。
9、回到Form窗體點擊報表右上角的小三角,選擇剛剛創建的報表Report1.rdlc,數據源默認的就可以,本人在這里沒有修改數據源名稱還是DataSet1.
10、后台代碼
public Form1()
{
InitializeComponent();
this.reportViewer1.Load -= new System.EventHandler(this.reportViewer1_Load);
}
private void button1_Click(object sender, EventArgs e)//button點擊事件
{
reportViewer1_Load(sender, e);
}
private void Form1_Load(object sender, EventArgs e)
{
this.reportViewer1.Load -= new System.EventHandler(this.reportViewer1_Load);
}
private void reportViewer1_Load(object sender, EventArgs e)
{
DataTable table = helps.GetDataTable("select ID,UserID,OID from User_Org where 1=1");
this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", table));
this.reportViewer1.RefreshReport();
}
運行程序加載:
點擊button
這里寫的是點擊button后才加載數據。