使用RDLC報表--使用自定義數據集

<!--[if !supportLists]-->1<!--[endif]-->新建窗體

 

<!--[if !supportLists]-->2<!--[endif]-->建立數據源

 

3<!--[endif]-->建立報表

 

新的數據報表已經生成,下面開始對數據源進行設置。

<!--[if !supportLists]-->4<!--[endif]-->對報表自動生成的數據源進行設置

選擇工具欄 à 報表 à 數據源,選中所要修改的數據源後,用“重命名”對其進行修改,如myds。

 

 

修改完成後,確定退出此窗口。

 

選中報表設計器內的表格,顯示屬性。將表格的數據集名稱更改爲上面修改的名稱。

 

如果一個報表文件內只有一個數據源,則表格內的數據值可直接寫爲“=Fields!字段.Value”的格式,如果包含多個數據源,則要對此字段的取值進行指定,如“=(Fields!字段.value,“數據集名稱””。

<!--[if !supportLists]-->5<!--[endif]-->手動生成數據源

手動生成的數據集內必須包含報表文件內設計的字段名稱,否則會運行出現錯誤。

生成數據集:

 

       /// <summary>

        
/// 報表執行操作

        
/// </summary>

        
/// <param name="sender"></param>

        
/// <param name="e"></param>

        private void button1_Click(object sender, EventArgs e)

        {

            //取得數據集

            string connstring = "Data Source=.;Initial Catalog=Northwind;Integrated Security=True";

            System.Data.SqlClient.SqlConnection conn1 = new System.Data.SqlClient.SqlConnection(connstring);

            System.Data.SqlClient.SqlCommand command1 = new System.Data.SqlClient.SqlCommand("select * from customers", conn1);

            System.Data.SqlClient.SqlDataAdapter ada1 = new System.Data.SqlClient.SqlDataAdapter(command1);

            DataSet c_ds = new DataSet();

            try

            {

                conn1.Open();

                ada1.Fill(c_ds);

            }

            finally

            {

                conn1.Close();

                command1.Dispose();

                conn1.Dispose();

            }

 

            //爲報表瀏覽器指定報表文件

            this.reportViewer1.LocalReport.ReportEmbeddedResource = "report.Report1.rdlc";

            //指定數據集,數據集名稱後爲表,不是DataSet類型的數據集

            this.reportViewer1.LocalReport.DataSources.Clear();

            this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("myds", c_ds.Tables[0]));

            //顯示報表

            this.reportViewer1.RefreshReport();

        }

 

運行後的數據顯示:

 

發佈了59 篇原創文章 · 獲贊 13 · 訪問量 22萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章