Telerik report showing same data for all columns created dynamically

狂风中的少年 提交于 2019-12-13 20:43:26

问题


I have created a telerik report using Visual Studio and set the Datasource from the DataTable. I am creating the columns dynamically at runtime using Telerik.Reporting.TableGroup. Now the problem I am having here is that the report showing the same data for all of the fields and when I debug it is setting different fields for different.

The code I am using is as follows:

private void Report4_NeedDataSource(object sender, EventArgs e)
{
  DataTable dt = new DataTable();
  dt = SalesReport.reportDataTable;
  table1.DataSource = dt;

  Telerik.Reporting.HtmlTextBox textboxGroup;
  Telerik.Reporting.HtmlTextBox textBoxTable;
  table1.ColumnGroups.Clear();
  table1.Body.Columns.Clear();
  table1.Body.Rows.Clear();
  int ColCount = dt.Columns.Count;

  for (int i = 0; i <= ColCount - 1; i++)
  {
    Telerik.Reporting.TableGroup tableGroupColumn = new Telerik.Reporting.TableGroup();
    table1.ColumnGroups.Add(tableGroupColumn);
    textboxGroup = new Telerik.Reporting.HtmlTextBox();
    textboxGroup.Style.BorderColor.Default = Color.Black;
    textboxGroup.Style.BorderStyle.Default = BorderType.Solid;
    textboxGroup.Value = dt.Columns[i].ColumnName;
    textboxGroup.Size = new SizeU(Unit.Inch(1.5), Unit.Inch(0.6));

    tableGroupColumn.ReportItem = textboxGroup;
    textBoxTable = new Telerik.Reporting.HtmlTextBox();
    textBoxTable.Value = "=Fields." + dt.Columns[i].ColumnName;
    textBoxTable.Size = new SizeU(Unit.Inch(1.1), Unit.Inch(0.3));
    table1.Body.SetCellContent(0, i, textBoxTable);
    table1.Items.AddRange(new ReportItemBase[] { textBoxTable, textboxGroup });

  }

}

回答1:


Not enough space in a comment unfortunately but here's my advice/suggestion. I'm not certain about your specific error, however in the past I have had issues when re-using variables. You declare your variable outside the for statement and it is possible that this is what is causing the problem.

private void Report4_NeedDataSource(object sender, EventArgs e)
{
  DataTable dt = new DataTable();
  dt = SalesReport.reportDataTable;
  table1.DataSource = dt;

  //Telerik.Reporting.HtmlTextBox textboxGroup; 
  //Telerik.Reporting.HtmlTextBox textBoxTable; 
  table1.ColumnGroups.Clear();
  table1.Body.Columns.Clear();
  table1.Body.Rows.Clear();
  int ColCount = dt.Columns.Count;

  for (int i = 0; i <= ColCount - 1; i++)
  {
    Telerik.Reporting.TableGroup tableGroupColumn = new Telerik.Reporting.TableGroup();
    table1.ColumnGroups.Add(tableGroupColumn);

    var textboxGroup = new Telerik.Reporting.HtmlTextBox();
    textboxGroup.Style.BorderColor.Default = Color.Black;
    textboxGroup.Style.BorderStyle.Default = BorderType.Solid;
    textboxGroup.Value = dt.Columns[i].ColumnName;
    textboxGroup.Size = new SizeU(Unit.Inch(1.5), Unit.Inch(0.6));
    tableGroupColumn.ReportItem = textboxGroup;

    var textBoxTable = new Telerik.Reporting.HtmlTextBox();
    textBoxTable.Value = "=Fields." + dt.Columns[i].ColumnName;
    textBoxTable.Size = new SizeU(Unit.Inch(1.1), Unit.Inch(0.3));
    table1.Body.SetCellContent(0, i, textBoxTable);

    table1.Items.AddRange(new ReportItemBase[] { textBoxTable, textboxGroup });
  }

}



回答2:


I suggest reading the following article. I found this same problem and my solution was to add unique names to the group column, label, and detail text boxes.

http://www.telerik.com/forums/incorrect-dynamic-table-columns

//Added
tableGroupColumn.Name = "group" + *something_uniquegoeshere*;

//Added
textboxGroup.Name = "label" +  *something_uniquegoeshere*;

//Added
textBoxTable.Name = "data" + *something_uniquegoeshere*;


来源:https://stackoverflow.com/questions/24693147/telerik-report-showing-same-data-for-all-columns-created-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!