| <!--[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(); } 運行後的資料顯示: |