Developers / Work with Reports using Code / Section Report / Bind a Section Report to Data / Bind Data Set and Unbound Data to Section Report at Run Time
Bind Data Set and Unbound Data to Section Report at Run Time

A Section report allows to specify a data set and unbound data, using a similar API. You need to just specify DataSet, DataTable, DataView, RowCollection as shown in the DataSource property. For DataSet, you should specify it as shown in the DataMember property.

In addition, a Section report allows to use any unbound data as a data source with the following script logic:

To use the IEnumerable data source

  1. Right-click the design surface and select View Code.
  2. Add the following code inside the class declaration of the report:
    Visual Basic.NET code. Paste inside the class declaration of the report.
    Copy Code
    Private datasource1 As IEnumerator(Of String) = Nothing 
    Dim list As List(Of String)= Nothing
    
    Visual Basic.NET code. Paste inside the class declaration of the report.
    Copy Code
    Private Function GetIEnumerableData() As IEnumerable(Of String)    
        For i As Integer = 1 To 10
           list.Add(String.Format("TestData_{0}", i.ToString()))       
        Next       
        Return list
    End Function
    
    C# code. Paste inside the class declaration of the report.
    Copy Code
    private IEnumerator<string> datasource = null;
    
    C# code. Paste inside the class declaration of the report.
    Copy Code
    private IEnumerable<string> GetIEnumerableData()
    {
        for (int i = 1; i <= 10; i++)
        {
            yield return string.Format("TestData_{0}", i.ToString());
        }
    }
    
  3. On the design surface, right-click the gray area around the design surface to select the report and select Properties.
  4. In the Properties window that appears, click the Events icon to view the available events for the report.
  5. Double-click the DataInitialize event. This creates an event-handling method for the report's DataInitialize event.
  6. Add the following code to the handler to add fields to the report's Fields collection.
    Visual Basic.NET code. Paste inside the DataInitialize event.
    Copy Code
    Me.Fields.Add("TestField")
    Me.list = New List(Of String)
    datasource1 = GetIEnumerableData().GetEnumerator()
    
    C# code. Paste inside the DataInitialize event.
    Copy Code
    this.Fields.Add("TestField");
    datasource = GetIEnumerableData().GetEnumerator();
    
  7. Repeat steps 3 and 4 to open the events list in the property window.
  8. Double-click the FetchData event. This creates an event-handling method for the report's FetchData event.
  9. Add code to the handler to retrieve information to populate the report fields.
    Visual Basic.NET code. Paste inside the FetchData event.
    Copy Code
    If datasource1.MoveNext() Then
                                            Me.Fields("TestField").Value = datasource1.Current
                                            eArgs.EOF = False
    Else
                                            eArgs.EOF = True
    End If
    
    C# code. Paste inside the FetchData event.
    Copy Code
    if (datasource.MoveNext())
    {
        this.Fields["TestField"].Value = datasource.Current;
        eArgs.EOF = false;
    }
    else
        eArgs.EOF = true;
    
Tip: In order to view the added data at run time, add controls to your report and assign their DataField property to the name of the fields you added in code while creating a field collection.