2017-06-13 89 views
0

在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不适用于我。

回答

0

在Telerik的论坛主题:http://www.telerik.com/forums/mvc-dynamic-columns

总之总结:他们在Telerik的的新版本改变的东西,这样就可以不修改的事件处理程序,如OnNeedDataSourceItemDataBinding报道:

报告活动不打算用作修改报告定义的地点/时间。截至2016年第三季度,处理阶段活动中报告项目定义的任何变化都不会对结果报告产生有效变化。当报表处理开始时,报表定义属性被读取和缓存。在以前的版本中,更改可能会生效,从而导致事件处理程序执行后处理的所有处理项的输出更改。

Telerik的支持,建议两个选项:

  1. 提供像其在包括与一些特定的标记生成的DataSource一劈定制ReportResolver
  2. 东西。

在我看来,第一种解决方案对于在运行时添加列这样的简单事情来说太复杂了。 第二种解决方案有点难看。

我解决这个情况,第三条道路:创建数据表和Telerik的所有可能的列的表,然后在数据表中的数据只需要填写栏,然后在此描述的Tekerik的桌子隐藏不必要的列:http://www.telerik.com/forums/need-to-hide-table-column-that-has-no-data