To make visible columns expand and fill the space of excluded (hidden) columns so that report width is maintained, work needs to be done is as under.
rdlc文件是一个xml文档 . 它将列宽定义为以下xml(这特定于使用Visual Studio 2005创建的RDLC文件,对于其他版本可能不同) . xml下面表示表中有6列 .
0.5in
1.125in
1in
1in
0.5in
1.375in
基本上逻辑很简单,只需增加可见列的宽度即可 . 但它的实现需要很少的代码行 .
计算隐藏列宽度的总和,然后重新计算可见列的宽度
float[] resizedwidth;
// code for recalculation goes here
将整个报告xml读入字符串变量'rptxml'
String rptxml = System.IO.File.ReadAllText(@"D:\SO\WinFormQ\WinFormQ\Report1.rdlc");
用修改后的xml段替换上面的xml段
int start = rptxml.IndexOf("");
int end = rptxml.IndexOf("") + "".Length;
String resizedcolumns = String.format(
""
+ "{0}in"
+ "{1}in"
+ "{2}in"
+ "{3}in"
+ "{4}in"
+ "{5}in"
+ ""
, resizedwidth[0], resizedwidth[1], resizedwidth[2], resizedwidth[3], resizedwidth[4], resizedwidth[5]
);
rptxml = rptxml.Substring(0, start) + resizedcolumns + rptxml.Substring(end);
从字符串变量'rptxml'创建TextReader
TextReader tr = new StringReader(rptxml);
使用LoadReportDefinition()方法加载修改后的报表定义
reportViewer1.LocalReport.LoadReportDefinition(tr);
继续指定DataSources和ReportParameters等,最后显示报告 . 注意:不要忘记关闭TextReader tr.Close() .