ComponentOne ReportViewer for ASP.NET Web Forms
Task-Based Help / Adding Dynamic Reports to ReportViewer
In This Topic
    Adding Dynamic Reports to ReportViewer
    In This Topic

    Dynamic reports can be created at run time. You can register dynamic report document from code-behind by using the static RegisterDocument method. The RegisterDocument method accepts two parameters: the first parameter is the name of the dynamic report document; the second parameter is a delegate, which will be called in order to generate report documents.

    Note: You can register several dynamic reports under different names and switch between these reports by changing the FileName property on the server side or the client side.

    The following example shows how to register and display the dynamic report document. Complete the following steps:

    1. In Source view, edit the <cc1:C1ReportViewer> markup so it appears like the following:

      To write code in Source View

       
      
      <cc1:C1ReportViewer runat="server" ID="C1ReportViewer1" FileName="InMemoryBasicTable" Zoom="75%" Height="475px" Width="100%">              
      </cc1:C1ReportViewer>
       
      
    2. Switch to Code view and add the following code to the Page_Load event so it appears like the following:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
          C1ReportViewer.RegisterDocument("InMemoryBasicTable", AddressOf BasicTable.MakeDoc)
      End Sub
      

      To write code in C#

      C#
      Copy Code
      protected void Page_Load(object sender, EventArgs e)
      {
          C1ReportViewer.RegisterDocument("InMemoryBasicTable", BasicTable.MakeDoc);
      }
      
    3. Add the following code just below the main class:

      To write code in Visual Basic

      Visual Basic
      Copy Code
       ''' Summary 
      
       ''' BasicTable in-memory document. 
      
       '''  Summary
      
      Public Class BasicTable
          Public Shared Function MakeDoc() As C1PrintDocument
              Dim doc As C1PrintDocument = C1ReportViewer.CreateC1PrintDocument()
       
              Dim rtxt1 As New RenderText(doc)
              rtxt1.Text = "This test shows the basic features of tables in C1PrintDocument:" & vbLf & vbTab & "- table borders (the GridLines style property, which allows to specify 4 outer and 2 inner lines);" & vbLf & vbTab & "- borders around individual cells and groups of cells;" & vbLf & vbTab & "- style attributes (including borders) for groups of disconnected cells;" & vbLf & vbTab & "- cells spanning rows and columns;" & vbLf & vbTab & "- content alignment within the cells (spanned or otherwise);" & vbLf & vbTab & "- table headers and footers;" & vbLf & vbTab & "- tags (such as page number/total page count) anywhere in the document (see the table footer);" & vbLf & vbTab & "- different style attributes including borders, font and background images." & vbLf & vbTab & "  " & vbLf
              rtxt1.Style.Font = New Font(rtxt1.Style.Font.FontFamily, 14)
              rtxt1.Style.Padding.Bottom = New C1.C1Preview.Unit("5mm")
              doc.Body.Children.Add(rtxt1)
       
              '
              ' make a table and fill all its cells with some demo data
              Dim rt1 As New RenderTable(doc)
              Const  ROWS As Integer = 100
              Const  COLS As Integer = 4
              For row As Integer = 0 To ROWS - 1
              For col As Integer = 0 To COLS - 1
                  Dim celltext As New RenderText(doc)
                  celltext.Text = String.Format("Cell ({0},{1})", row, col)
                  ' Note that rt1.Cells[row, col] will create cells on demand -
                  ' no need to specify the number of rows/cols initially.
                  rt1.Cells(row, col).RenderObject = celltext
              Next
              Next
              ' add the table to the document
              doc.Body.Children.Add(rt1)
       
              '
              ' unlike the old print-doc, in the new one changes can be made at any
              ' point in the program, and they will be reflected in the document when
              ' it is rendered. Add some customizations to the table:
       
              '
              ' by default, tables have no borders. add a simple border:
              rt1.Style.GridLines.All = New LineDef("2pt", Color.DarkGray)
              rt1.Style.GridLines.Horz = New LineDef("1pt", Color.Blue)
              rt1.Style.GridLines.Vert = New LineDef("1pt", Color.Brown)
       
              '
              ' table headers and footers
       
              ' add a table header:
              ' setup the header as a whole
              rt1.RowGroups(0, 2).PageHeader = True
              rt1.RowGroups(0, 2).Style.BackColor = Color.DarkOrchid
              ' multiple inheritance supported in styles: the text color from the
              ' group's style will merge with the font from the cell's own style:
              rt1.RowGroups(0, 2).Style.TextColor = Color.LemonChiffon
              rt1.RowGroups(0, 2).Style.GridLines.All = New LineDef("2pt", Color.DarkCyan)
              rt1.RowGroups(0, 2).Style.GridLines.Horz = LineDef.Empty
              rt1.RowGroups(0, 2).Style.GridLines.Vert = LineDef.Empty
              ' setup specific cells in the header:
              DirectCast(rt1.Cells(0, 0).RenderObject, RenderText).Text = "Header row 0"
              DirectCast(rt1.Cells(1, 0).RenderObject, RenderText).Text = "Header row 1"
              rt1.Cells(0, 1).SpanCols = 2
              rt1.Cells(0, 1).SpanRows = 2
              rt1.Cells(0, 1).RenderObject = New RenderText(doc)
              DirectCast(rt1.Cells(0, 1).RenderObject, RenderText).Text = "Multi-row table headers and footers are supported"
              rt1.Cells(0, 1).Style.TextAlignHorz = AlignHorzEnum.Center
              rt1.Cells(0, 1).Style.Font = New Font("Arial", 14, FontStyle.Bold)
       
              ' setup a table footer
              rt1.RowGroups(rt1.Rows.Count - 2, 2).PageFooter = True
              rt1.RowGroups(rt1.Rows.Count - 2, 2).Style.BackColor = Color.LemonChiffon
              rt1.Cells(rt1.Rows.Count - 2, 0).SpanRows = 2
              rt1.Cells(rt1.Rows.Count - 2, 0).SpanCols = rt1.Cols.Count - 1
              rt1.Cells(rt1.Rows.Count - 2, 0).Style.TextAlignHorz = AlignHorzEnum.Center
              rt1.Cells(rt1.Rows.Count - 2, 0).Style.TextAlignVert = AlignVertEnum.Center
              DirectCast(rt1.Cells(rt1.Rows.Count - 2, 0).RenderObject, RenderText).Text = "This is a table footer."
              rt1.Cells(rt1.Rows.Count - 2, 3).SpanRows = 2
              rt1.Cells(rt1.Rows.Count - 2, 3).Style.TextAlignHorz = AlignHorzEnum.Right
              ' tags (such as page no/page count) can be inserted anywhere in the document
              DirectCast(rt1.Cells(rt1.Rows.Count - 2, 3).RenderObject, RenderText).Text = "Page [PageNo] of [PageCount]"
       
              '
              ' in tables, Style.Borders merges seamlessly into the table grid lines:
       
              ' it is easy to put a border around specific cells:
              rt1.Cells(8, 3).Style.Borders.All = New LineDef("3pt", Color.OrangeRed)
              DirectCast(rt1.Cells(8, 3).RenderObject, RenderText).Text = "It is easy to put a border around a single cell using cell.Style.Borders"
       
              '
              ' cells can be combined into groups, and their styles can be manipulated as
              ' a single entity:
       
              ' define a group of cells by specifying the rectangles bounding the cells:
              DirectCast(rt1.Cells(3, 2).RenderObject, RenderText).Text = "Cells can be combined into groups to be manipulated as a single entity " & "(such as all cells with the pale green background in this table)."
              rt1.Cells(3, 2).SpanCols = 2
              rt1.Cells(3, 2).SpanRows = 3
              Dim cells1 As Rectangle() = New Rectangle() {New Rectangle(2, 3, 2, 3), New Rectangle(0, 10, 3, 2), New Rectangle(1, 23, 2, 4), New Rectangle(1, 36, 1, 24), New Rectangle(0, 72, 3, 6)}
              Dim grp1 As New UserCellGroup(cells1)
              grp1.Style.BackColor = Color.PaleGreen
              grp1.Style.Font = New Font("Arial", 12, FontStyle.Bold)
              grp1.Style.Borders.All = New LineDef("2pt", Color.DarkGreen)
              rt1.UserCellGroups.Add(grp1)
       
       
              ' row/col span
              DirectCast(rt1.Cells(14, 1).RenderObject, RenderText).Text = "Column and row spans are fully supported, as well as alignment within the (spanned or not) cells."
              rt1.Cells(14, 1).SpanCols = 3
              rt1.Cells(14, 1).SpanRows = 5
              rt1.Cells(14, 1).Style.Font = New Font("Arial", 12, FontStyle.Bold Or FontStyle.Italic)
              rt1.Cells(14, 1).Style.Borders.All = New LineDef("2pt", Color.DarkOrange)
              rt1.Cells(14, 1).Style.TextAlignHorz = AlignHorzEnum.Center
              rt1.Cells(14, 1).Style.TextAlignVert = AlignVertEnum.Center
              rt1.RowGroups(14, 5).SplitBehavior = SplitBehaviorEnum.Never
       
              Return doc
          End Function
      End Class
      

      To write code in C#

      C#
      Copy Code
      /// Summary
      
      
      
      
      
      
      /// BasicTable in-memory document. 
      
      
      
      
      
      /// Summary
      
      public class BasicTable
      {
          static public C1PrintDocument MakeDoc()
          {
              C1PrintDocument doc = C1ReportViewer.CreateC1PrintDocument();
       
              RenderText rtxt1 = new RenderText(doc);
              rtxt1.Text = "This test shows the basic features of tables in C1PrintDocument:\n"
              + "\t- table borders (the GridLines style property, which allows to specify 4 outer and 2 inner lines);\n"
              + "\t- borders around individual cells and groups of cells;\n"
              + "\t- style attributes (including borders) for groups of disconnected cells;\n"
              + "\t- cells spanning rows and columns;\n"
              + "\t- content alignment within the cells (spanned or otherwise);\n"
              + "\t- table headers and footers;\n"
              + "\t- tags (such as page number/total page count) anywhere in the document (see the table footer);\n"
              + "\t- different style attributes including borders, font and background images.\n"
              + "\t  \n"
              ;
              rtxt1.Style.Font = new Font(rtxt1.Style.Font.FontFamily, 14);
              rtxt1.Style.Padding.Bottom = new C1.C1Preview.Unit("5mm");
              doc.Body.Children.Add(rtxt1);
       
              //
              // make a table and fill all its cells with some demo data
              RenderTable rt1 = new RenderTable(doc);
              const int ROWS = 100;
              const int COLS = 4;
              for (int row = 0; row < ROWS; ++row)
              {
              for (int col = 0; col < COLS; ++col)
              {
                  RenderText celltext = new RenderText(doc);
                  celltext.Text = string.Format("Cell ({0},{1})", row, col);
                  // Note that rt1.Cells[row, col] will create cells on demand -
                  // no need to specify the number of rows/cols initially.
                  rt1.Cells[row, col].RenderObject = celltext;
              }
              }
              // add the table to the document
              doc.Body.Children.Add(rt1);
       
              //
              // unlike the old print-doc, in the new one changes can be made at any
              // point in the program, and they will be reflected in the document when
              // it is rendered. Add some customizations to the table:
       
              //
              // by default, tables have no borders. add a simple border:
              rt1.Style.GridLines.All = new LineDef("2pt", Color.DarkGray);
              rt1.Style.GridLines.Horz = new LineDef("1pt", Color.Blue);
              rt1.Style.GridLines.Vert = new LineDef("1pt", Color.Brown);
       
              //
              // table headers and footers
       
              // add a table header:
              // setup the header as a whole
              rt1.RowGroups[0, 2].PageHeader = true;
              rt1.RowGroups[0, 2].Style.BackColor = Color.DarkOrchid;
              // multiple inheritance supported in styles: the text color from the
              // group's style will merge with the font from the cell's own style:
              rt1.RowGroups[0, 2].Style.TextColor = Color.LemonChiffon;
              rt1.RowGroups[0, 2].Style.GridLines.All = new LineDef("2pt", Color.DarkCyan);
              rt1.RowGroups[0, 2].Style.GridLines.Horz = LineDef.Empty;
              rt1.RowGroups[0, 2].Style.GridLines.Vert = LineDef.Empty;
              // setup specific cells in the header:
              ((RenderText)rt1.Cells[0, 0].RenderObject).Text = "Header row 0";
              ((RenderText)rt1.Cells[1, 0].RenderObject).Text = "Header row 1";
              rt1.Cells[0, 1].SpanCols = 2;
              rt1.Cells[0, 1].SpanRows = 2;
              rt1.Cells[0, 1].RenderObject = new RenderText(doc);
              ((RenderText)rt1.Cells[0, 1].RenderObject).Text = "Multi-row table headers and footers are supported";
              rt1.Cells[0, 1].Style.TextAlignHorz = AlignHorzEnum.Center;
              rt1.Cells[0, 1].Style.Font = new Font("Arial", 14, FontStyle.Bold);
       
              // setup a table footer
              rt1.RowGroups[rt1.Rows.Count - 2, 2].PageFooter = true;
              rt1.RowGroups[rt1.Rows.Count - 2, 2].Style.BackColor = Color.LemonChiffon;
              rt1.Cells[rt1.Rows.Count - 2, 0].SpanRows = 2;
              rt1.Cells[rt1.Rows.Count - 2, 0].SpanCols = rt1.Cols.Count - 1;
              rt1.Cells[rt1.Rows.Count - 2, 0].Style.TextAlignHorz = AlignHorzEnum.Center;
              rt1.Cells[rt1.Rows.Count - 2, 0].Style.TextAlignVert = AlignVertEnum.Center;
              ((RenderText)rt1.Cells[rt1.Rows.Count - 2, 0].RenderObject).Text = "This is a table footer.";
              rt1.Cells[rt1.Rows.Count - 2, 3].SpanRows = 2;
              rt1.Cells[rt1.Rows.Count - 2, 3].Style.TextAlignHorz = AlignHorzEnum.Right;
              // tags (such as page no/page count) can be inserted anywhere in the document
              ((RenderText)rt1.Cells[rt1.Rows.Count - 2, 3].RenderObject).Text = "Page [PageNo] of [PageCount]";
       
              //
              // in tables, Style.Borders merges seamlessly into the table grid lines:
       
              // it is easy to put a border around specific cells:
              rt1.Cells[8, 3].Style.Borders.All = new LineDef("3pt", Color.OrangeRed);
              ((RenderText)rt1.Cells[8, 3].RenderObject).Text =
              "It is easy to put a border around a single cell using cell.Style.Borders";
       
              //
              // cells can be combined into groups, and their styles can be manipulated as
              // a single entity:
       
              // define a group of cells by specifying the rectangles bounding the cells:
              ((RenderText)rt1.Cells[3, 2].RenderObject).Text =
              "Cells can be combined into groups to be manipulated as a single entity " +
              "(such as all cells with the pale green background in this table).";
              rt1.Cells[3, 2].SpanCols = 2;
              rt1.Cells[3, 2].SpanRows = 3;
              Rectangle[] cells1 = new Rectangle[] {
                      new Rectangle(2, 3, 2, 3),
                      new Rectangle(0, 10, 3, 2),
                      new Rectangle(1, 23, 2, 4),
                      new Rectangle(1, 36, 1, 24),
                      new Rectangle(0, 72, 3, 6),
                      };
              UserCellGroup grp1 = new UserCellGroup(cells1);
              grp1.Style.BackColor = Color.PaleGreen;
              grp1.Style.Font = new Font("Arial", 12, FontStyle.Bold);
              grp1.Style.Borders.All = new LineDef("2pt", Color.DarkGreen);
              rt1.UserCellGroups.Add(grp1);
       
       
              // row/col span
              ((RenderText)rt1.Cells[14, 1].RenderObject).Text =
              "Column and row spans are fully supported, as well as alignment within the (spanned or not) cells.";
              rt1.Cells[14, 1].SpanCols = 3;
              rt1.Cells[14, 1].SpanRows = 5;
              rt1.Cells[14, 1].Style.Font = new Font("Arial", 12, FontStyle.Bold | FontStyle.Italic);
              rt1.Cells[14, 1].Style.Borders.All = new LineDef("2pt", Color.DarkOrange);
              rt1.Cells[14, 1].Style.TextAlignHorz = AlignHorzEnum.Center;
              rt1.Cells[14, 1].Style.TextAlignVert = AlignVertEnum.Center;
              rt1.RowGroups[14, 5].SplitBehavior = SplitBehaviorEnum.Never;
       
              return doc;
          }
      }
      

      This code demonstrates some of the features in C1PrintDocument tables.

    What You've Accomplished

    Run the application and at run time click one of the links to resize the parent container containing the C1ReportViewer. Notice that the C1ReportViewer control resizes with the parent container.