在MVC报告中动态创建表列存在问题。情况如下。tekerik报告mvc动态列
在我的Report.cs
文件中,我覆盖OnNeedDataSource()
方法。在其中创建System.Data.DataTable
,然后将此DataTable作为DataSource附加到Telerik的表中。
代码此方法:
/// <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 });
}
}
但是,作为结果,我实现了从我的DataTable的第一科拉姆值在我Telerik的表中的每一列:
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不适用于我。