轉(zhuǎn)帖|使用教程|編輯:龔雪|2017-02-03 11:37:02.000|閱讀 1080 次
概述:
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
1.建立一個(gè)WinForm窗體,從工具箱中拉入控件
SimpleButton,DocumentViewer,SplitContainerControl,LabelControl,TextEdit,GroupControl
2. 創(chuàng)建一個(gè)Devexpress XtraReport報(bào)表文件
3. 在報(bào)表中建立一個(gè)ReportHeader
4. 右鍵單擊報(bào)表文件空白區(qū)域,添加一個(gè)從報(bào)表DetailReport
5. 在ReportHeader 拉入一個(gè)XRLabel 修改Text屬性為:主從報(bào)表,并在Detail和DetailReport區(qū)分別拉入一個(gè)XRTable控件(XRTable默認(rèn)三列)
6. 創(chuàng)建數(shù)據(jù)源表
7. 處理相對(duì)應(yīng)的主從表關(guān)系綁定
a,按鈕事件
private void btnShowReport_Click(object sender, EventArgs e) { DataSet ds = BindRpt(); MasterDetailRpt Rpt = new MasterDetailRpt(ds); this.documentViewer1.DocumentSource = Rpt; Rpt.CreateDocument(); }
b,獲取數(shù)據(jù)源,并設(shè)置兩個(gè)表主從關(guān)系。
private DataSet BindRpt() { DataSet ds = new DataSet(); try { //連接數(shù)據(jù)源,給Rpt綁定數(shù)據(jù)源,包含兩個(gè)數(shù)據(jù)表 SqlConnection con = new SqlConnection("Data Source=(local);Integrated Security=SSPI;Initial Catalog=ReportDeom"); SqlDataAdapter adapter; con.Open(); //表1 SqlCommand cmd = new SqlCommand("SELECT * FROM Dept where dept_name=@dept_name OR @dept_name='' ", con); SqlParameter[] paras = new SqlParameter[]{ new SqlParameter("@dept_name",txtdept_name.Text) }; cmd.Parameters.AddRange(paras); adapter = new SqlDataAdapter(cmd); adapter.Fill(ds, "Dept"); //表2 SqlCommand cmd2 = new SqlCommand("select * FROM Users where dept_id in (select dept_id from Dept where dept_name=@dept_name OR @dept_name='') ", con); SqlParameter[] paras2 = new SqlParameter[]{ new SqlParameter("@dept_name",txtdept_name.Text) }; cmd2.Parameters.AddRange(paras2); adapter = new SqlDataAdapter(cmd2); adapter.Fill(ds, "User"); con.Close(); //給數(shù)據(jù)集建立主外鍵關(guān)系(主從表) DataColumn ParentColumn = ds.Tables["Dept"].Columns["dept_id"]; DataColumn ChildColumn = ds.Tables["User"].Columns["dept_id"]; DataRelation Rel = new DataRelation("RelationColumn", ParentColumn, ChildColumn); ds.Relations.Add(Rel); } catch (Exception ex) { throw ex; } return ds; }
c,綁定數(shù)據(jù)到報(bào)表文件
public MasterDetailRpt(DataSet ds) { InitializeComponent(); //綁定主表 this.DataSource = ds; this.DataMember = "Dept"; this.xrTableCell3.DataBindings.Add("Text", ds, "Dept.dept_name"); DetailReport.DataMember = "RelationColumn"; //綁定從表 DetailReport.DataSource = ds; this.xrTableCell1.DataBindings.Add("Text", ds, "RelationColumn.user_id"); this.xrTableCell2.DataBindings.Add("Text", ds, "RelationColumn.username"); }
(參考來源:cnblogs-Mr.Ming)
想要了解更多DevExpress使用技巧?海量視頻課程以及最專業(yè)的DevExpress培訓(xùn)盡在。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn