telerik 报告 mvc 动态列
tekerik report mvc dynamic columns
在 MVC 报告中动态创建 table 列存在问题。情况如下。
在我的 Report.cs
文件中,我覆盖了 OnNeedDataSource()
方法。我在其中创建 System.Data.DataTable
,然后将此 DataTable 作为数据源附加到 Telerik 的 table。
此方法的代码:
/// <summary>
/// Creates DataTable from ReportRecords
/// </summary>
/// <param name="data">List of ReportRecords</param>
/// <returns>DataTable filled with ReportRecord's values</returns>
private System.Data.DataTable CreateDataTable(List<ReportRecord> data)
{
System.Data.DataColumn currentColumn = new System.Data.DataColumn("Current", typeof(int));
System.Data.DataColumn lateColumn = new System.Data.DataColumn("Late", typeof(int));
System.Data.DataColumn foreignColumn = new System.Data.DataColumn("Foreign", typeof(int));
System.Data.DataTable table = new System.Data.DataTable("table");
table.Columns.AddRange(new System.Data.DataColumn[] { currentColumn, lateColumn, foreignColumn });
foreach (ReportRecord reportRecord in data)
{
System.Data.DataRow row = table.NewRow();
row["Current"] = reportRecord.Current;
row["Late"] = reportRecord.Late;
row["Foreign"] = reportRecord.Foreign;
table.Rows.Add(row);
}
return table;
}
/// <summary>
/// Assign DataTable as DataSource of dinamically created table in report
/// </summary>
/// <param name="table">DataTable with data to display</param>
private void AddTableToReport(System.Data.DataTable table)
{
this.tableMain.DataSource = table;
//create two HtmlTextBox items (one for header and one for data) which would be added to the items collection of the table
Telerik.Reporting.TextBox textboxGroup;
Telerik.Reporting.TextBox textBoxTable;
//we do not clear the Rows collection, since we have a details row group and need to create columns only
this.tableMain.ColumnGroups.Clear();
this.tableMain.Body.Columns.Clear();
this.tableMain.Body.Rows.Clear();
int i = 0;
this.tableMain.ColumnHeadersPrintOnEveryPage = true;
foreach (System.Data.DataColumn dc in table.Columns)
{
Telerik.Reporting.TableGroup tableGroup = new Telerik.Reporting.TableGroup();
Telerik.Reporting.TableGroup tableGroup2 = new Telerik.Reporting.TableGroup();
tableGroup.ChildGroups.Add(tableGroup2);
this.tableMain.ColumnGroups.Add(tableGroup);
this.tableMain.Body.Columns.Add(new Telerik.Reporting.TableBodyColumn(Unit.Inch(1)));
textboxGroup = new Telerik.Reporting.TextBox();
textboxGroup.Style.BorderColor.Default = Color.Black;
textboxGroup.Style.BorderStyle.Default = BorderType.Solid;
textboxGroup.Value = dc.ColumnName;
textboxGroup.Size = new SizeU(Unit.Inch(1.1), Unit.Inch(0.3));
tableGroup.ReportItem = textboxGroup;
textBoxTable = new Telerik.Reporting.TextBox();
textBoxTable.Style.BorderColor.Default = Color.Black;
textBoxTable.Style.BorderStyle.Default = BorderType.Solid;
textBoxTable.Value = "=Fields." + dc.ColumnName;
textBoxTable.Size = new SizeU(Unit.Inch(1.1), Unit.Inch(0.3));
this.tableMain.Body.SetCellContent(0, i++, textBoxTable);
this.tableMain.Items.AddRange(new ReportItemBase[] { textBoxTable, textboxGroup });
}
}
但是,结果是,我在我的 Telerik 的每一列中都获得了 DataTable 第一列的值 table:
Current Late Foreign
0 0 0
20 20 20
1 1 1
21 21 21
4 4 4
但值应如下所示:
Current Late Foreign
0 0 1
20 1 0
1 0 6
21 0 0
4 1 0
为什么会出现这种情况,如何解决?
此外:Telerik report showing same data for all columns created dynamically 对我不起作用。
Telerik 论坛的主题:http://www.telerik.com/forums/mvc-dynamic-columns
简而言之:他们在较新版本的 Telerik 中进行了一些更改,因此您无法修改事件处理程序中的报告,例如 OnNeedDataSource
或 ItemDataBinding
:
Report Events are not intended to be used as a place/time to modify the report definition. As of Q3 2016 any changes on the report items' definitions in processing stage events will not make effective change in the resulting report. Report definition properties are read and cached when the report processing gets started. In previous versions changes may take effect, resulting in changed output for all processing items processed after the event handler execution.
Telerik 的支持人员提出了两个选项:
- 提供自定义
ReportResolver
- 有点像 hack,包括生成带有特定标记的 DataSource。
在我看来,第一个解决方案对于在运行时添加列这样简单的事情来说太复杂了。
第二种解决方案有点难看。
我用第三种方式解决了这种情况:用所有可能的列创建 DataTable 和 Telerik 的 table,然后在 DataTable 中只填充必要的列,然后在 Tekerik 的 table 中隐藏不需要的列作为此处描述:http://www.telerik.com/forums/need-to-hide-table-column-that-has-no-data
在 MVC 报告中动态创建 table 列存在问题。情况如下。
在我的 Report.cs
文件中,我覆盖了 OnNeedDataSource()
方法。我在其中创建 System.Data.DataTable
,然后将此 DataTable 作为数据源附加到 Telerik 的 table。
此方法的代码:
/// <summary>
/// Creates DataTable from ReportRecords
/// </summary>
/// <param name="data">List of ReportRecords</param>
/// <returns>DataTable filled with ReportRecord's values</returns>
private System.Data.DataTable CreateDataTable(List<ReportRecord> data)
{
System.Data.DataColumn currentColumn = new System.Data.DataColumn("Current", typeof(int));
System.Data.DataColumn lateColumn = new System.Data.DataColumn("Late", typeof(int));
System.Data.DataColumn foreignColumn = new System.Data.DataColumn("Foreign", typeof(int));
System.Data.DataTable table = new System.Data.DataTable("table");
table.Columns.AddRange(new System.Data.DataColumn[] { currentColumn, lateColumn, foreignColumn });
foreach (ReportRecord reportRecord in data)
{
System.Data.DataRow row = table.NewRow();
row["Current"] = reportRecord.Current;
row["Late"] = reportRecord.Late;
row["Foreign"] = reportRecord.Foreign;
table.Rows.Add(row);
}
return table;
}
/// <summary>
/// Assign DataTable as DataSource of dinamically created table in report
/// </summary>
/// <param name="table">DataTable with data to display</param>
private void AddTableToReport(System.Data.DataTable table)
{
this.tableMain.DataSource = table;
//create two HtmlTextBox items (one for header and one for data) which would be added to the items collection of the table
Telerik.Reporting.TextBox textboxGroup;
Telerik.Reporting.TextBox textBoxTable;
//we do not clear the Rows collection, since we have a details row group and need to create columns only
this.tableMain.ColumnGroups.Clear();
this.tableMain.Body.Columns.Clear();
this.tableMain.Body.Rows.Clear();
int i = 0;
this.tableMain.ColumnHeadersPrintOnEveryPage = true;
foreach (System.Data.DataColumn dc in table.Columns)
{
Telerik.Reporting.TableGroup tableGroup = new Telerik.Reporting.TableGroup();
Telerik.Reporting.TableGroup tableGroup2 = new Telerik.Reporting.TableGroup();
tableGroup.ChildGroups.Add(tableGroup2);
this.tableMain.ColumnGroups.Add(tableGroup);
this.tableMain.Body.Columns.Add(new Telerik.Reporting.TableBodyColumn(Unit.Inch(1)));
textboxGroup = new Telerik.Reporting.TextBox();
textboxGroup.Style.BorderColor.Default = Color.Black;
textboxGroup.Style.BorderStyle.Default = BorderType.Solid;
textboxGroup.Value = dc.ColumnName;
textboxGroup.Size = new SizeU(Unit.Inch(1.1), Unit.Inch(0.3));
tableGroup.ReportItem = textboxGroup;
textBoxTable = new Telerik.Reporting.TextBox();
textBoxTable.Style.BorderColor.Default = Color.Black;
textBoxTable.Style.BorderStyle.Default = BorderType.Solid;
textBoxTable.Value = "=Fields." + dc.ColumnName;
textBoxTable.Size = new SizeU(Unit.Inch(1.1), Unit.Inch(0.3));
this.tableMain.Body.SetCellContent(0, i++, textBoxTable);
this.tableMain.Items.AddRange(new ReportItemBase[] { textBoxTable, textboxGroup });
}
}
但是,结果是,我在我的 Telerik 的每一列中都获得了 DataTable 第一列的值 table:
Current Late Foreign
0 0 0
20 20 20
1 1 1
21 21 21
4 4 4
但值应如下所示:
Current Late Foreign
0 0 1
20 1 0
1 0 6
21 0 0
4 1 0
为什么会出现这种情况,如何解决?
此外:Telerik report showing same data for all columns created dynamically 对我不起作用。
Telerik 论坛的主题:http://www.telerik.com/forums/mvc-dynamic-columns
简而言之:他们在较新版本的 Telerik 中进行了一些更改,因此您无法修改事件处理程序中的报告,例如 OnNeedDataSource
或 ItemDataBinding
:
Report Events are not intended to be used as a place/time to modify the report definition. As of Q3 2016 any changes on the report items' definitions in processing stage events will not make effective change in the resulting report. Report definition properties are read and cached when the report processing gets started. In previous versions changes may take effect, resulting in changed output for all processing items processed after the event handler execution.
Telerik 的支持人员提出了两个选项:
- 提供自定义
ReportResolver
- 有点像 hack,包括生成带有特定标记的 DataSource。
在我看来,第一个解决方案对于在运行时添加列这样简单的事情来说太复杂了。 第二种解决方案有点难看。
我用第三种方式解决了这种情况:用所有可能的列创建 DataTable 和 Telerik 的 table,然后在 DataTable 中只填充必要的列,然后在 Tekerik 的 table 中隐藏不需要的列作为此处描述:http://www.telerik.com/forums/need-to-hide-table-column-that-has-no-data