Developer Express 之 XtraReport如何動態綁定資料

來源:互聯網
上載者:User

XtraReport報表編輯器裡的儲存,是可以儲存為一個檔案的,所以它應該提供了一個從檔案載入的方法,這時我們可以發現XtraReport裡有一個LoadLayout的方法,可以載入報表檔案,它的重載方法是可以從IO.Stream裡載入報表檔案,也就是說,我們可以進一步的把這個報表範本以二進位的方式儲存在資料庫裡。需要的時候,從資料庫調用即可。

我們在設計XtraReport的模板的時候,重寫它的析構方法,一個有傳入資料來源的,一個沒有。代碼如下:

      public XtraReport1()        {            InitializeComponent();        } //資料預覽是有用        public XtraReport1(DataSet ds)//建構函式重載        {            InitializeComponent();            SetDataBind(ds);        }        private void SetDataBind(DataSet ds)//綁定資料來源        {            DataSource=ds;               this.xrTableCell4.DataBindings.Add("Text", DataSource, "test1");            this.xrTableCell5.DataBindings.Add("Text", DataSource, "test1");        }
填充資料代碼如下:
        private void simpleButton1_Click(object sender, System.EventArgs e)        {            XtraReport1  xrpt1=new XtraReport1(GetTempDataSet());            xrpt1.ShowPreviewDialog();        }        private DataSet GetTempDataSet()        {            DataSet ds=new DataSet();            DataTable dt=new DataTable("tempTable");            dt.Columns.Add("test1",Type.GetType("System.String"));            dt.Columns.Add("test",Type.GetType("System.String"));            dt.Columns.Add("test3",Type.GetType("System.String"));            dt.Columns.Add("test4",Type.GetType("System.String"));            dt.Columns.Add("test5",Type.GetType("System.String"));            dt.Columns.Add("test6",Type.GetType("System.String"));            dt.Columns.Add("test7",Type.GetType("System.String"));            DataRow tempRow;                          for (int i=0;i<7;i++)//i:Row            {                tempRow=dt.NewRow();                tempRow[0]=i.ToString();                tempRow[1]=i.ToString();                tempRow[2]=i.ToString();                tempRow[3]=i.ToString();                tempRow[4]=i.ToString();                tempRow[5]=i.ToString();                tempRow[6]=i.ToString();                dt.Rows.Add(tempRow);            }            ds.Tables.Add(dt);            return ds;        }
其中關於主從表的話,因為傳進去的DaTaSet所以我們可以再外面把相應的關係指定好後加到DaTaSet。
下面給出主從表的代碼:
                DataColumn parentColumns;                DataColumn childColumns;                parentColumns = ds.Tables["Suppliers"].Columns["SupplierID"];                childColumns = ds.Tables["Products"].Columns["SupplierID"];                DataRelation dsdr1 = new DataRelation("fk_1", parentColumns, childColumns);                ds.Relations.Add(dsdr1);                DataRelation dsdr2 = new DataRelation("fk_2", ds.Tables["Products"].Columns["ProductID"], ds.Tables["OrderDetails"].Columns["ProductID"]);                ds.Relations.Add(dsdr2);

而模板中綁定資料的話跟上面單表是的大同小異,但是裡面的綁定要記得引用的是你綁定關係的外鍵,也就是,如下代碼:

        public XtraReport1(DataSet ds)        {            InitializeComponent();            SetDataBing(ds);        }        private void SetDataBing(DataSet ds)        {                      DataMember = "Suppliers";            DataSource = ds;            this.cellCompanyName.DataBindings.Add("Text", DataSource, "Suppliers.CompanyName");            this.sContadName.DataBindings.Add("Text", DataSource, "Suppliers.ContadName");            this.sCountry.DataBindings.Add("Text", DataSource, "Suppliers.Country");            this.sContactTitle.DataBindings.Add("Text", DataSource, "Suppliers.ContactTitle");            this.sRegion.DataBindings.Add("Text", DataSource, "Suppliers.Region");            this.sPhone.DataBindings.Add("Text", DataSource, "Suppliers.Phone");            this.sCity.DataBindings.Add("Text", DataSource, "Suppliers.City");            this.sFax.DataBindings.Add("Text", DataSource, "Suppliers.Fax");            this.sPostalCode.DataBindings.Add("Text", DataSource, "Suppliers.PostalCode");            this.sHomePage.DataBindings.Add("Text", DataSource, "Suppliers.HomePage");            this.sAddress.DataBindings.Add("Text", DataSource, "Suppliers.Address");            this.sCompanyName.DataBindings.Add("Text", DataSource, "Suppliers.CompanyName");            DetailReport.DataMember = "fk_1";            DetailReport.DataSource = DataSource;            this.pProductName.DataBindings.Add("Text", DataSource, "fk_1.ProductName");            this.pProductID.DataBindings.Add("Text", DataSource, "fk_1.ProductID");            this.pCategory.DataBindings.Add("Text", DataSource, "fk_1.CategoryID");            this.pUnit.DataBindings.Add("Text", DataSource, "fk_1.QuantityPerUnit");            this.pUnitPrice.DataBindings.Add("Text", DataSource, "fk_1.UnitPrice");            this.pDiscontinued.DataBindings.Add("Text", DataSource, "fk_1.Discontinued");            DetailReport1.DataMember = "fk_1.fk_2";            DetailReport1.DataSource = DataSource;            this.oOrderID.DataBindings.Add("Text", DataSource, "fk_1.fk_2.OrderID");            this.oQuantity.DataBindings.Add("Text", DataSource, "fk_1.fk_2.Quantity");            this.oDiscount.DataBindings.Add("Text", DataSource, "fk_1.fk_2.Discount");            this.oUnitPrice.DataBindings.Add("Text", DataSource, "fk_1.fk_2.UnitPrice");                    }
this.pProductName.DataBindings.Add("Text", DataSource, "fk_1.ProductName")以這句來說明,"fk_1.ProductName"
這個就是子表的資料欄位,這部是主從表動態綁定的關鍵,這樣資料就會隨主表資料變化而變化。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.