Asp.Net(c#)中使用ReportViewer控件制作報表


環境:VS2010

1、新建一個Default.aspx文件,把ReportViewer1控件和ScriptManager1拖到頁面上。

(1)、從ReportViewer1控件的任務欄中選擇設計新報表,出現”報表向導“對話框。

依次設置如下:

“數據集屬性”,選擇或設置數據集。

“排列字段”,將相應字段拖放到行組、列組、值字段對應的框中。​

“選擇布局”,根據需要需要選擇相應布局。​

“選擇樣式”,選擇喜歡的樣式,完成報表向導。

(2)、把ReportViewer1控件的選擇報表選為Report1.rdlc

(3)、調整報表布局或根據需要重新設計報表。

2、在Default.aspx.cs頁面中,添加代碼把報表顯示出來。​

protected void Button1_Click(object sender, EventArgs e)

{

//連接數據庫

SqlConnection myconn = new SqlConnection();

myconn.ConnectionString = strConn;

//執行查詢

string selStr = "select dwmc,gwmc,zprs from gwb";

SqlDataAdapter da = new SqlDataAdapter(selStr, myconn);

DataSet ds = new DataSet();

da.Fill(ds);

//建立數據源

ReportDataSource rds = new ReportDataSource("DataSet1", ds.Tables[0]);

ReportViewer1.Visible = true;

ReportViewer1.LocalReport.DataSources.Clear();

ReportViewer1.LocalReport.DataSources.Add(rds);

ReportViewer1.LocalReport.Refresh();

}​

注意:​

(1)、要導入以下命名空間:​​

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

using Microsoft.Reporting.WebForms;​

(2)、​定義連接字符串變量

public string strConn = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;​​

(3)、代碼中報表數據源的數據集名稱(上面紅字部分)必須與報表模板中所用的DataSet數據集名稱一致。

3、運行結果如下:​

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM