天天看點

asp.net中輕量級報表RDLC的動态資料綁定

 asp.net中的輕量級報表RDLC我最近一直在研究。資料源的動态綁定我一直覺得過于複雜,網上提供的都是以下模式:

    //設定為本地報表

      // this.ReportViewer1.ProcessingMode = ProcessingMode.Local;(可選)

        //指定報表路徑

        ReportViewer1.LocalReport.ReportPath = MapPath("Report1.rdlc");

        // ReportViewer1.LocalReport.DataSources.Clear();(可選)

        DataSet ds= Getdata();     

        //設定資料源

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

        ReportViewer1.LocalReport.DataSources.Add(rds);

        ReportViewer1.LocalReport.Refresh();

事實上,報表顯示格式是固定的。那也就是說報表原來的資料源可以不作大的改動,理論上直接修改查詢就可以了。

但是網上一直找不到這種解決方式,我經過多次測試,發現這樣解決是比較簡潔的(VS 2005團體開發版下測試,資料庫平台為SQL Server 2005,作業系統為Windows XP sp3):

//修改資料源

SqlDataSource1.SelectCommand = "SELECT KY_JC.JCID, KY_JC.CGLB, KY_JC.CGMC, KY_JC.CBSMC, KY_JC.CBH, KY_JC.CBRQ, KY_JC.JB, KY_JCZB.XM, KY_JCZB.BM, KY_JCZB.CDZS, KY_JCZB.FZ FROM KY_JC INNER JOIN KY_JCZB ON KY_JC.JCID = KY_JCZB.JCID where BM=" + "'"+Session["bm"]+"'";

//重新綁定     

ReportViewer1.DataBind();

//重新整理報表

ReportViewer1.LocalReport.Refresh();

這裡需要注意的是,如果把清楚報表資料源的代碼加上就會出錯,因為沒有添加新的資料源,是以不需要清除,直接重新綁定即可。