昨天發表了Devexpress XtraReports系列開篇,今天我們繼續。
今天的主題是創建表格報表。
首先我們來看看最后實現的效果。Demo最后附上。
接下來開始講解如何一步一步做出這個報表:
第一步,創建如上窗體,拉入控件SimpleButton,DocumentViewer,SplitContainerControl,LabelControl,TextEdit,GroupControl,簡單布局我就不多說了,跟上篇一樣的布局 ,詳見:http://www.cnblogs.com/lhmlyx2723356/p/3286101.html
第二步,創建一個報表文件。如圖:
在報表中拉入兩個XRTable控件,在ReportHeader放置一個,並修改其Text屬性如上圖,姓名,密碼,創建時間,備注。
在Detail區放置一個XRTable。至於對齊,字體修改,相信大家也都能找到相對應的屬性去修改。
這里要講一下報表的隔行換色的屬性設置,如圖:
選中Detail中的XRTable后,在屬性窗口中根據需要設置BackColor屬性。
第三步,我們利用數據庫做一張簡單的表並輸入一些測試值。如圖:
第四步,數據庫表,布局都做好了,接下來我們就來處理相對應的事件了。輸入文本框的值,點擊按鈕把查詢出來的數據源綁定到報表中
a,修改報表文件的構造函數,讓它在實例化的時候可以接收數據源。
public TableRpt(DataSet ds)//修改構造函數
{
InitializeComponent();
this.DataSource = ds;
this.xrTableCell5.DataBindings.Add("Text", ds, "name");//為單元格綁定數據源中相對應的字段
this.xrTableCell6.DataBindings.Add("Text", ds, "password");
this.xrTableCell8.DataBindings.Add("Text", ds, "createdate");
this.xrTableCell7.DataBindings.Add("Text", ds, "remark");
}
b,獲取數據源
private DataSet BindRpt()
{
DataSet ds = new DataSet();
try
{
SqlConnection con = new SqlConnection("Data Source=(local);Integrated Security=SSPI;Initial Catalog=ReportDeom");
SqlDataAdapter adapter;
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM TableRptData where name=@name OR @name='' ", con);
SqlParameter[] paras = new SqlParameter[]{
new SqlParameter("@name",txtName.Text.Trim())
};
cmd.Parameters.AddRange(paras);
adapter = new SqlDataAdapter(cmd);
adapter.Fill(ds, "TableRpt"); ;
con.Close();
}
catch (Exception ex)
{
throw ex;
}
return ds;
}
c,單擊按鈕,綁定數據到報表中
private void btnShowReport_Click(object sender, EventArgs e)
{
DataSet ds = BindRpt();
TableRpt Rpt = new TableRpt(ds);
this.documentViewer1.DocumentSource = Rpt;
Rpt.CreateDocument();
}
到此,報表就表格報表就完成了。
希望對初學者有點幫助。謝謝。
最后又是送福利時間,哈哈。。屌絲們。。。雄起吧。。。