原創|其它|編輯:郝浩|2012-09-06 15:16:07.000|閱讀 6615 次
概述:使用xtraReport進行報表部分的開發,由于網上的資料比較少,而且大部分示例是xtraReport集成到程序中,目前需求要使用獨立設計的報表文件.repx,數據使用IList,這樣涉及到在設計報表時,如何給報表文件中字段賦值,尤其是分組時指定字段,這里根據官方文檔和自己的測試,得到了一個方法,這里做具體說明。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
使用背景:DevExpress控件,版本v10.2,開發環境vs2010;報表單獨進行設計,即單獨的repx文件(使用report desinger設計,dev的demo里有),使用時動態載入并顯示,數據源使用List<>;使用xtraReport進行報表部分的開發,由于網上的資料比較少,而且大部分示例是xtraReport集成到程序中,目前需求要使用獨立設計的報表文件.repx,數據使用IList,這樣涉及到在設計報表時,如何給報表文件中字段賦值,尤其是分組時指定字段(原來在版本v8時,可以在沒有數據源的情況下輸入分組的字段,在v10.2版本中已經行不通,必須指定數據源的字段才行,暈死)只好自己結合官方文檔和幫助加自己的測試,得到以下的方法,如果哪些同學有更好的方法,我們可以共同探討:
下面我只寫個簡單的示例,基本思想都一樣。
一、創建winform程序,放置一個按鈕控件,一個dev組件中的printcontrol控件,記得項目引用
DevExpress.XtraEditors;
DevExpress.XtraReports;
主要代碼:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//定義報表
XtraReport xr = new XtraReport();
//定義測試數據model
public class testdata
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public int Age {get;set;}
}
//顯示報表
private void button1_Click(object sender, EventArgs e)
{
//得到list數據 下面是測試用,實際中需要通過函數得到
List<testdata> Items = new List<testdata>();
testdata item;
for (int i = 1; i <= 5; i++)
{
item = new testdata();
item.Id = i;
item.Name = "Name"+i.ToString();
item.Address = "第" + i.ToString() + "區";
item.Age = 10+i;
Items.Add(item);
}
for (int i = 1; i <= 5; i++)
{
item = new testdata();
item.Id = i;
item.Name = "Name-" + i.ToString();
item.Address = "第-" + i.ToString() + "區";
item.Age = 20+i;
Items.Add(item);
}
//動態載入報表文件
xr.LoadLayout(Application.StartupPath + "\\newtest.repx");
xr.DataSource = Items;
XRControl xc; //報表上的組件
xc = xr.FindControl("lbl_Unit", true);
if (xc != null)
{ (xc as XRLabel).Text = "使用部門名稱"; } //動態指定報表某個顯示的名稱,當然你
也可以在報表中寫好
xc = xr.FindControl("GroupHeader1", true);
if (xc != null) //動態指定分組的列,因為在v10.2報表設計器中,已經不能輸入,必須
指定數據源的字段
{
(xc as DevExpress.XtraReports.UI.GroupHeaderBand).GroupFields.Add(new
GroupField("Id"));
}
//分組綁定合計并指定格式,不加不行
xc = xr.FindControl("tableCellSumAge", true);
if (xc != null)
{
(xc as XRTableCell).DataBindings.Add("Text", xr.DataSource, "Age",
"{0:N2}");
}
this.printControl1.PrintingSystem = xr.PrintingSystem; //將報表的打印系統賦給
窗體上的打印控件
xr.CreateDocument();
}
}
二、使用dev的報表設計器 report Designer 創建一個報表,增加reportHeader、details、
groupHeader、groupFooter等需要的band及table和label,并在相應位置放置 XRTable或xrLabel以顯示數據,具體怎么個布局就不說了,看需求了;其中XRTableCell主要來顯示List的數據,放在detail區域中,只要直接指定其Text屬性即可 如 [Name],對于金額字段 還要指定其summary的formatString屬性{0:N2},對應上面的年齡合計字段 tableCellSumAge,設置其summary的running為group(組合計,report:整個報表的合計,none當然就不合計)。
先寫這里,東西不多,但研究卻花了不少時間,希望對用到的同學有所幫助。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:轉自snowman2010 CSDN