第一步
简历一个winform窗体,从工具箱中拉入控件 SimpleButton,DocumentViewer,SplitContainerControl,LabelControl,TextEdit,GroupControl,简单布局(见效果图)我就不多阐述了。
第二步
创建一个Devexpress Xtrareport报表文件如图:
在报表中建立一个ReportHreder,如图
在报表文件空白区域右键,然后添加一个从报表detailReport,如图
接着在ReportHeader拉入一个XRLabel修改Text属性为主从报表,然后在Detail和DetailReport区分别拉入XRTable
至此 报表布局就完成了。
第三步
创建数据源,
1 //获取数据集 2 private DataSet Getdata() 3 { 4 DataSet ds = new DataSet(); 5 SqlDataAdapter dt; 6 string constr = "server=192.168.100.222;user=sa;pwd=p@ssw1rd;database=ArchivesMenagement"; 7 SqlConnection mycon = new SqlConnection(constr); 8 try 9 { 10 mycon.Open(); 11 //表一 12 SqlCommand mycom = new SqlCommand("select * from ArchivesInfo", mycon); 13 dt = new SqlDataAdapter(mycom); 14 dt.Fill(ds, "ArchivesInfo"); 15 //表2 16 SqlCommand two = new SqlCommand("select * from Department", mycon); 17 dt = new SqlDataAdapter(two); 18 dt.Fill(ds, "Department"); 19 mycon.Close(); 20 //给数据集创建主外键关系 21 DataColumn parent = ds.Tables["ArchivesMenagement"].Columns["DeId"]; 22 DataColumn child = ds.Tables["Department"].Columns["DeId"]; 23 //数据集写入关系 24 DataRelation Rel = new DataRelation("RelationColumn", parent, child); 25 ds.Relations.Add(Rel); 26 } 27 catch (Exception ex) 28 { 29 30 MessageBox.Show(ex.Message); 31 } 32 33 return ds; 34 35 } 36 //实例化report 37 private void simpleButton1_Click(object sender, EventArgs e) 38 { 39 XtraReport1 report = new XtraReport1(Getdata()); 40 report.Landscape = true; 41 documentViewer1.DocumentSource = report; 42 report.CreateDocument(); 43 44 45 }
1 //Report Designer 重载 2 public XtraReport1( DataSet ds) 3 { 4 InitializeComponent(); 5 //绑定主表 6 this.DataSource = ds; 7 this.DataMember = "ArchivesInfo"; 8 this.xrTableCell1.DataBindings.Add("Text",ds,"DeId"); 9 //绑定从表 10 this.DataSource = ds; 11 this.xrTableCell2.DataBindings.Add("Text",ds,"RelationColumn.DeId"); 12 this.xrTableCell3.DataBindings.Add("Text", ds, "RelationColumn.DeName"); 13 14 }