我要做的报表模板要求是传入一个DataTble,不管datatable的列数多少,计算列宽后显示报表
这是我的报表:
灰色那个XRtable是显示列标题的,下面那个是显示绑定数据的
下面是报表页面代码:
代码语言:javascript复制namespace OlenoUI.Report
{
public partial class XtraReport1 :DevExpress.XtraReports.UI.XtraReport
{
public XtraReport1()
{
InitializeComponent();
}
public XtraReport1(DataTable ds)//构造函数重载
{
InitializeComponent();
DataTable dt = ds.Clone();
foreach (DataRow dr in ds.Rows)
dt.ImportRow(dr);
SetDataBind(dt);
}
private void SetDataBind(DataTable table)//绑定数据源
{
if (table==null||table.Columns.Count < 1) return;
if (table.Columns[0].ColumnName == "id")
{
table.Columns.Remove("id");
}
string lastColName=table.Columns[table.Columns.Count - 1].ColumnName;
if (lastColName == "初盘"||lastColName=="复盘")
{
table.Columns.Remove(table.Columns[table.Columns.Count - 1].ColumnName);
table.Columns.Add(lastColName);
foreach (DataRow row in table.Rows)
{
row[table.Columns[lastColName].ColumnName] = "囗";
}
}
DataSource = table; ///
int colWidth = this.PageWidth-this.Margins.Left-this.Margins.Right;
this.taday.Text = DateTime.Today.ToString("yyyy-MM-dd");
colWidth = colWidth / table.Columns.Count;
try
{
foreach (DataColumn dc in table.Columns)
{
XRTableCell[] colomnTittle = xrTable2.InsertColumnToLeft(xrTableCell2);
colomnTittle[0].Text = dc.ColumnName;
colomnTittle[0].Width = colWidth;
XRTableCell[] columnsDetail = xrTable1.InsertColumnToLeft(xrTableCell1);
columnsDetail[0].Text = dc.ColumnName;
columnsDetail[0].Width = colWidth;
//绑定数据 参数含义分别为:绑定对应的属性名称(绑定columnsDetail[0]的Text属性),数据源,绑定对应的数据列
//还有 XRLabel也可以这样绑定数据哦
columnsDetail[0].DataBindings.Add("Text", DataSource, dc.ColumnName);
}
this.count.Text = CheckMain.Count;
this.checkMen.Text = CheckMain.CheckMen;
this.tittle.Text = CheckMain.Tittle;
xrTable2.DeleteColumn(xrTableCell2);
xrTable1.DeleteColumn(xrTableCell1);
}
catch { }
}
}
}
以上报表只有传递一个DataTable就可以调用了
XtraReport1 reprot=new XtraReport1(dt); dt是以查询出结果的DataTable
实现效果:
各位别吐槽,界面设计没花大工夫
横向打印:this.Landscape = true; //
定义纸张:this.PaperKind = System.Drawing.Printing.PaperKind.A4Plus; //设置纸张
然后遇到超复杂动态表头报表时我是通过拼接XRTable方式拼命计算位置组成的,