首先看下布局designer
细节:
分组一定要用到GroupHeather
设置好有 右边会出现
接下来是代码部分
Form1中代码
using DevExpress.XtraReports.UI; private DataSet getds() { DataSet ds = new DataSet(); string constr = "server=192.168.100.222;user=sa;pwd=p@ssw1rd;database=pwd1"; SqlConnection mycon = new SqlConnection(constr); try { mycon.Open(); SqlCommand mycom = new SqlCommand("select * from mulittb", mycon); SqlDataAdapter dpt = new SqlDataAdapter(mycom); dpt.Fill(ds, "mulittb"); mycon.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } return ds; } //显示 private void simpleButton1_Click(object sender, EventArgs e) { DataSet ds = getds(); XtraReport1 report = new XtraReport1(ds); report.Landscape = true; documentViewer1.DocumentSource = report; report.CreateDocument(); } //预览 private void simpleButton2_Click(object sender, EventArgs e) { DataSet ds = getds(); XtraReport1 report = new XtraReport1(ds); report.Landscape = true; report.ShowPreviewDialog(); }
Xtrareport代码
public XtraReport1(DataSet ds) { InitializeComponent(); this.DataSource = ds; this.DataMember = "mulittb"; xrTableCell1.DataBindings.Add("Text",ds,"type"); //设置groupHeader分组字段 (type ,报表列 排序类型.上升asc) 可以不写 GroupField fz = new GroupField("type",XRColumnSortOrder.Ascending); //把分组字段添加进GroupHeader1 ((GroupHeaderBand)(this.FindControl("GroupHeader1", true))).GroupFields.Add(fz); //绑定显示多列的字段 this.xrTableCell2.DataBindings.Add("Text",ds,"name"); }