This is a migrated thread and some comments may be shown as answers.

Add subtotal dynamically create table in telerik

1 Answer 256 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
saravana
Top achievements
Rank 1
saravana asked on 09 Dec 2016, 12:46 PM
i Need to Add subtotal dynamically created table in telerik Report pls help

1 Answer, 1 is accepted

Sort by
0
Stef
Telerik team
answered on 12 Dec 2016, 05:32 PM
Hi saravana,

Below is a code snippet that evaluates data and creates a Table item's template at run-time. The generated Table item is bound to the data object. There is also a static row group that will display totals.

The code is generated by VS Report Designer and it is adjusted for usage at run-time:
public DataTable GetData()
      {
          dynamic table = new DataTable();
          table.Columns.Add("col1", typeof(int));
          table.Columns.Add("col2", typeof(int));
          table.Rows.Add(1, 3);
          table.Rows.Add(4, 6);
          table.Rows.Add(2, 5);
          table.Rows.Add(4, 7);
 
          return table;
      }
 
      public Telerik.Reporting.Report GenerateReport()
      {
 
          //create a blank report
          Telerik.Reporting.Report report = new Telerik.Reporting.Report();
          report.Name = "Report1";
          report.PageSettings.Margins = new Telerik.Reporting.Drawing.MarginsU(Telerik.Reporting.Drawing.Unit.Inch(1.0), Telerik.Reporting.Drawing.Unit.Inch(1.0), Telerik.Reporting.Drawing.Unit.Inch(1.0), Telerik.Reporting.Drawing.Unit.Inch(1.0));
          report.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.Letter;
          report.Width = Telerik.Reporting.Drawing.Unit.Inch(6.0);
 
          //create a detail section and add it to the report instance's Items collection
          Telerik.Reporting.DetailSection detailSection = new Telerik.Reporting.DetailSection();
          detailSection.Height = Telerik.Reporting.Drawing.Unit.Inch(1);
 
          detailSection.Name = "DetailSection";
          report.Items.AddRange(new Telerik.Reporting.ReportItemBase[] { detailSection });
 
          //create a blank Table item
          Telerik.Reporting.Table table1 = new Telerik.Reporting.Table();
          table1.Location = new Telerik.Reporting.Drawing.PointU(Telerik.Reporting.Drawing.Unit.Inch(0.0), Telerik.Reporting.Drawing.Unit.Inch(0.0));
          table1.Name = "Table1";
          table1.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(4), Telerik.Reporting.Drawing.Unit.Inch(1));
 
          //get the data for the table
          dynamic data = GetData();
          table1.DataSource = data;
 
          //create a dynamic row group
          Telerik.Reporting.TableGroup DetailRowGroup = new Telerik.Reporting.TableGroup();
          DetailRowGroup.Groupings.Add(new Telerik.Reporting.Grouping(null));
          DetailRowGroup.Name = "DetailRowGroup";
          table1.RowGroups.Add(DetailRowGroup);
          //add a row container
          table1.Body.Rows.Add(new Telerik.Reporting.TableBodyRow(Telerik.Reporting.Drawing.Unit.Inch(0.5)));
 
 
          //add columns
          for (int i = 0; i <= data.Columns.Count - 1; i++)
          {
              //add a column container
              table1.Body.Columns.Add(new Telerik.Reporting.TableBodyColumn(Telerik.Reporting.Drawing.Unit.Inch(2)));
              //add a static column group per data field
              Telerik.Reporting.TableGroup columnGroup = new Telerik.Reporting.TableGroup();
              table1.ColumnGroups.Add(columnGroup);
 
              //header textbox
              Telerik.Reporting.TextBox headerTextBox = new Telerik.Reporting.TextBox();
              headerTextBox.Name = "headerTextBox" + i.ToString();
              headerTextBox.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(2), Telerik.Reporting.Drawing.Unit.Inch(0.5));
              headerTextBox.Value = data.Columns[i].ColumnName;
              headerTextBox.Style.BackgroundColor = Color.Yellow;
              headerTextBox.Style.BorderStyle.Default = Telerik.Reporting.Drawing.BorderType.Solid;
            //  headerTextBox.Style.BorderWidth.Default = Telerik.Reporting.Drawing.Unit.Pixel(1);
              columnGroup.ReportItem = headerTextBox;
 
              //field that will be displayed
              Telerik.Reporting.TextBox detailRowTextBox = new Telerik.Reporting.TextBox();
              detailRowTextBox.Name = "detailRowTextBox" + i.ToString();
              detailRowTextBox.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(2), Telerik.Reporting.Drawing.Unit.Inch(0.5));
              detailRowTextBox.Value = "= Fields.[" + data.Columns[i].ColumnName + "]";
              table1.Body.SetCellContent(0, i, detailRowTextBox);
 
              //add the nested items in the Table.Items collection
              table1.Items.AddRange(new Telerik.Reporting.ReportItemBase[] {
          headerTextBox,
          detailRowTextBox
      });
          }
 
          //add total group - static group out of the detail row group
          table1.Body.Rows.Add(new Telerik.Reporting.TableBodyRow(Telerik.Reporting.Drawing.Unit.Inch(0.5)));
          Telerik.Reporting.TableGroup totalRowGroup = new Telerik.Reporting.TableGroup();
          totalRowGroup.Name = "totalRowGroup";
          table1.RowGroups.Add(totalRowGroup);
          for (int i = 0; i <= data.Columns.Count - 1; i++)
          {
              Telerik.Reporting.TextBox totalRowTextBox = new Telerik.Reporting.TextBox();
              totalRowTextBox.Name = "detailRowTextBox" + i.ToString();
              totalRowTextBox.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(2), Telerik.Reporting.Drawing.Unit.Inch(0.5));
              totalRowTextBox.Value = "='Total:'+ Sum(Fields.[" + data.Columns[i].ColumnName + "])";
 
              totalRowTextBox.Style.BackgroundColor = Color.Azure;
              table1.Body.SetCellContent(1, i, totalRowTextBox);
          }
          //add the table in the detail section's Items collection
          detailSection.Items.AddRange(new Telerik.Reporting.ReportItemBase[] { table1 });
 
          return report;
      }


Regards,
Stef
Telerik by Progress
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
saravana
Top achievements
Rank 1
Answers by
Stef
Telerik team
Share this question
or