Search results
PDF

How to generate the RDL and RDLC reports programmatically in the report viewer

The Bold Reports reporting library allows you to generate and preview RDL and RDLC reports programmatically in the report viewer. The ReportDefinition class is defined as a report object model. In the report definition instance, you can create, add, and modify the report properties, report sections such as header and footer, and report items.

The following steps illustrates how to create a basic report object model:

Initialize a report definition

The following code snippet guides you in initializing the report definition.

ReportDefinition CreateReport()
{
 ReportDefinition report = new ReportDefinition();
 report.ReportSections = new ReportSections();
 var reportSection = new ReportSection();
 report.ReportSections.Add(reportSection);
 reportSection.Width = new BoldReports.RDL.DOM.Size("6in");
 report.ReportUnitType = "Inch";
 report.RDLType = RDLType.RDL2010;
 return report;
}

BoldReports.RDL.DOM.Style AddStyle()
{
 BoldReports.RDL.DOM.Style style = new BoldReports.RDL.DOM.Style();
 style.Border = new BoldReports.RDL.DOM.Border();
 style.Border.Width = new BoldReports.RDL.DOM.Size("1pt");
 style.Border.Style = "Solid";
 style.Border.Color = "Black";
 return style;
}

Create a Data source for report

The following code snippet guides you in creating a Datasource for the report and setting values to their properties.

ReportDefinition CreateReport()
{
…
…
…
reportSection.Page = new BoldReports.RDL.DOM.Page();
reportSection.Page.Style = AddStyle();
reportSection.Page.PageHeight = "4in";
reportSection.Page.PageWidth = "6in";
var newDataSource = CreateDataSource("DataSource1", "SQL", "Data Source =.; Initial Catalog = <database>");
report.DataSources = new DataSources();
report.DataSources.Add(newDataSource);
…
…
…
}
//If you are using RDLC report then you can pass any string value as connection string
BoldReports.RDL.DOM.DataSource CreateDataSource(string name, string dataProvider, string connectionString)
{
 var dataSource = new BoldReports.RDL.DOM.DataSource();
 dataSource.Name = name;
 dataSource.SecurityType = BoldReports.RDL.DOM.SecurityType.Integrated;
 dataSource.ConnectionProperties = new BoldReports.RDL.DOM.ConnectionProperties();
 dataSource.ConnectionProperties.DataProvider = dataProvider;
 dataSource.ConnectionProperties.ConnectString = connectionString;
 dataSource.ConnectionProperties.IntegratedSecurity = true;

 return dataSource;
}

Create a Dataset for the report

The following code snippet guides you in creating a Dataset for the report and setting values to their properties.

ReportDefinition CreateReport()
{
…
…
…
reportSection.Page.PageWidth = "6in";
var newDataSource = CreateDataSource("DataSource1", "SQL", "Data Source =.; Initial Catalog = <database>");
report.DataSources = new DataSources();
report.DataSources.Add(newDataSource);
var newDataSet = CreateDataSet("DataSource1", "DataSet1");
report.DataSets = new DataSets();
report.DataSets.Add(newDataSet);
…
…
…
}

BoldReports.RDL.DOM.DataSet CreateDataSet(string dataSourceName, string dataSetName)
{
    var dataSet = new BoldReports.RDL.DOM.DataSet();
    dataSet.Name = dataSetName;
    dataSet.Query = new Query();
    dataSet.Query.DataSourceName = dataSourceName;
    dataSet.Query.CommandText = "SELECT HumanResources.Department.DepartmentID,HumanResources.Department.Name,HumanResources.Department.GroupName,HumanResources.Department.ModifiedDate FROM HumanResources.Department";
    dataSet.Query.QueryDesignerState = new QueryDesignerState();
    var table = CreateTable();
    dataSet.Query.QueryDesignerState.Tables = new List<Table>();
    dataSet.Query.QueryDesignerState.Tables.Add(table);
    dataSet.Fields = new Fields();
    dataSet.Fields.Add(CreateField("DepartmentID", "System.Int16"));
    dataSet.Fields.Add(CreateField("Name", "System.String"));
    dataSet.Fields.Add(CreateField("GroupName", "System.String"));
    dataSet.Fields.Add(CreateField("ModifiedDate", "System.DateTime"));

    return dataSet;
}

BoldReports.RDL.DOM.Table CreateTable()
{
    var table = new Table();
    table.Schema = "HumanResources";
    table.Name = "Department";
    table.Columns = new List<Column>();
    table.Columns.Add(CreateColumn("DepartmentID"));
    table.Columns.Add(CreateColumn("Name"));
    table.Columns.Add(CreateColumn("GroupName"));
    table.Columns.Add(CreateColumn("ModifiedDate"));
    return table;
}

BoldReports.RDL.DOM.Column CreateColumn(string columnName)
{
    var column = new Column();
    column.Name = columnName;
    return column;
}

BoldReports.RDL.DOM.Field CreateField(string fieldName, string type)
{
    var field = new Field();
    field.Name = fieldName;
    field.TypeName = type;
    field.DataField = fieldName;
    return field;
}

Create a report page header

The following code snippet guides you in creating a PageHeader report section and setting values to their properties.

ReportDefinition CreateReport()
{
…
…
…
reportSection.Page = new BoldReports.RDL.DOM.Page();
reportSection.Page.Style = AddStyle();
reportSection.Page.PageHeight = "4in";
reportSection.Page.PageWidth = "6in";
reportSection.Page.PageHeader = CreateHeader();
…
…
…
}

PageHeader CreateHeader()
{
 PageHeader pageHeader = new PageHeader();
 pageHeader.Height = new BoldReports.RDL.DOM.Size("0.59167in");
 pageHeader.Style = AddStyle();
 pageHeader.PrintOnFirstPage = true;
 pageHeader.PrintOnLastPage = true;
 pageHeader.ReportItems = new ReportItems();
 var textBox = CreateTextBox("TextBox2", "Header", "1.61333in", "0.13833in", "2.2in", "0.34167in");
 pageHeader.ReportItems.Add(textBox);
 return pageHeader;
}

The following code snippet guides you in creating a PageFooter report section and setting values to their properties.

ReportDefinition CreateReport()
{
…
…
…
reportSection.Page.PageHeader = CreateFooter();
…
…
…
}

PageFooter CreateFooter()
{
 PageFooter pageFooter = new PageFooter();
 pageFooter.Style = AddStyle();
 pageFooter.Height = new BoldReports.RDL.DOM.Size("0.59167in");
 pageFooter.ReportItems = new ReportItems();
 pageFooter.PrintOnFirstPage = true;
 pageFooter.PrintOnLastPage = true;
 var textBox = CreateTextBox("TextBox3", "Footer", "1.61333in", "0.05416in", "2.2in", "0.34167in");
 pageFooter.ReportItems.Add(textBox);
 return pageFooter;
}

Initialize a report body section

  • Initialize a report body object and set the values to their properties like height, styles, and report items as shown in the following code snippet.
ReportDefinition CreateReport()
{
…
…
…
reportSection.Body = CreateBody();
…
…
…
}

Body CreateBody()
{
 var body = new Body();
 body.Height = new BoldReports.RDL.DOM.Size("2.03333in");
 body.Style = AddStyle();
 body.ReportItems = new ReportItems();
 var textBox = CreateTextBox("TextBox1", "Body", "1.58833in", "0.66333in", "2.225in", "0.59167in");
 body.ReportItems.Add(textBox);
 return body;
}
  • Using the following code snippets, you can create a Textbox report item and assign the values for their properties like name, styles, paragraphs, and dimension values.
PageHeader CreateHeader()
{
 …
 …
 …
 pageHeader.ReportItems = new ReportItems();
 var textBox = CreateTextBox("TextBox2", "Header", "1.61333in", "0.13833in", "2.2in", "0.34167in");
 pageHeader.ReportItems.Add(textBox);
 …
 …
 …
}

PageFooter CreateFooter()
{
 …
 …
 …
 pageFooter.ReportItems = new ReportItems();
 var textBox = CreateTextBox("TextBox3", "Footer", "1.61333in", "0.05416in", "2.2in", "0.34167in");
 pageFooter.ReportItems.Add(textBox);
 …
 …
 …
}

Body CreateBody()
{
 …
 …
 …
 body.ReportItems = new ReportItems();
 var textBox = CreateTextBox("TextBox1", "Body", "1.58833in", "0.66333in", "2.225in", "0.59167in");
 body.ReportItems.Add(textBox);
 …
 …
 …
}

BoldReports.RDL.DOM.TextBox CreateTextBox(string name, string text, string left, string top, string width, string height)
{
 var textBox = new BoldReports.RDL.DOM.TextBox();
 textBox.Name = name;
 textBox.Height = new BoldReports.RDL.DOM.Size(height);
 textBox.Width = new BoldReports.RDL.DOM.Size(width);
 textBox.Left = new BoldReports.RDL.DOM.Size(left);
 textBox.Top = new BoldReports.RDL.DOM.Size(top);
 textBox.Style = new BoldReports.RDL.DOM.Style();
 textBox.Style.TextAlign = "Center";
 textBox.Style.VerticalAlign = "Top";

 textBox.Paragraphs = new Paragraphs();
 BoldReports.RDL.DOM.Paragraph paragraph = new BoldReports.RDL.DOM.Paragraph();
 TextRuns runs = new TextRuns();
 TextRun run = new TextRun();
 run.Style = new BoldReports.RDL.DOM.Style();
 run.Style.FontStyle = "Default";
 run.Style.TextAlign = "Center";
 run.Style.FontFamily = "Arial";
 run.Style.FontSize = new BoldReports.RDL.DOM.Size("10pt");
 run.Value = text;
 runs.Add(run);
 paragraph.Style = new BoldReports.RDL.DOM.Style();
 paragraph.Style.VerticalAlign = "Top";
 paragraph.Style.TextAlign = "Center";
 paragraph.TextRuns = runs;
 textBox.Paragraphs.Add(paragraph);

 return textBox;
}

Create a Table for the report

  • Using the following code snippets, you can create a Table report item and assign the values for their properties like name, styles, paragraphs, and dimension values.
BoldReports.RDL.DOM.Tablix CreateTablix(string name, string text, string left, string top, string width, string height)
{
    var tableItem = new BoldReports.RDL.DOM.Tablix();
    tableItem.Name = name;
    tableItem.Height = new BoldReports.RDL.DOM.Size(height);
    tableItem.Width = new BoldReports.RDL.DOM.Size(width);
    tableItem.Left = new BoldReports.RDL.DOM.Size(left);
    tableItem.Top = new BoldReports.RDL.DOM.Size(top);
    tableItem.Style = new BoldReports.RDL.DOM.Style();
    tableItem.Style.Border = new BoldReports.RDL.DOM.Border();
    tableItem.Style.Border.Style = "Solid";
    tableItem.DataSetName = "DataSet1";

    tableItem.TablixBody = new TablixBody();
    tableItem.TablixBody.TablixColumns = new TablixColumns();
    tableItem.TablixBody.TablixColumns.Add(CreateTablixColumn());
    tableItem.TablixBody.TablixColumns.Add(CreateTablixColumn());
    tableItem.TablixBody.TablixColumns.Add(CreateTablixColumn());

    tableItem.TablixBody.TablixRows = new TablixRows();
    var rowOne = new List<TablixRowValues>();
    rowOne.Add(new TablixRowValues { TextBoxName = "TextBox1", TextBoxValue = "Department ID" });
    rowOne.Add(new TablixRowValues { TextBoxName = "Textbox3", TextBoxValue = "Name" });
    rowOne.Add(new TablixRowValues { TextBoxName = "Textbox5", TextBoxValue = "Group Name" });
    tableItem.TablixBody.TablixRows.Add(CreateTablixRow(rowOne));
    var rowTwo = new List<TablixRowValues>();
    rowTwo.Add(new TablixRowValues { TextBoxName = "DepartmentID", TextBoxValue = "=Fields!DepartmentID.Value" });
    rowTwo.Add(new TablixRowValues { TextBoxName = "Name", TextBoxValue = "=Fields!Name.Value" });
    rowTwo.Add(new TablixRowValues { TextBoxName = "GroupName", TextBoxValue = "=Fields!GroupName.Value" });
    tableItem.TablixBody.TablixRows.Add(CreateTablixRow(rowTwo));

    tableItem.TablixColumnHierarchy = new TablixColumnHierarchy();
    tableItem.TablixColumnHierarchy.TablixMembers = new TablixMembers();
    tableItem.TablixColumnHierarchy.TablixMembers.Add(new TablixMember());
    tableItem.TablixColumnHierarchy.TablixMembers.Add(new TablixMember());
    tableItem.TablixColumnHierarchy.TablixMembers.Add(new TablixMember());
    tableItem.TablixRowHierarchy = new TablixRowHierarchy();
    tableItem.TablixRowHierarchy.TablixMembers = new TablixMembers();
    tableItem.TablixRowHierarchy.TablixMembers.Add(new TablixMember() { KeepWithGroup = KeepWithGroup.After });
    tableItem.TablixRowHierarchy.TablixMembers.Add(CreateTablixMember("Details"));

    return tableItem;
}

BoldReports.RDL.DOM.TablixColumn CreateTablixColumn()
{
    var tablixColumn = new TablixColumn();
    tablixColumn.Width = "1in";

    return tablixColumn;
}

BoldReports.RDL.DOM.TablixRow CreateTablixRow(List<TablixRowValues> values)
{
    var tablixRow = new TablixRow();
    tablixRow.Height = "0.25in";
    tablixRow.TablixCells = new TablixCells();
    for(int i = 0; i < values.Count; i++)
    {
        tablixRow.TablixCells.Add(CreateTablixCell(values[i].TextBoxName, values[i].TextBoxValue));
    }

    return tablixRow;
}

BoldReports.RDL.DOM.TablixCell CreateTablixCell(string textBoxName, string textBoxValue)
{
    var tablixCell = new TablixCell();
    tablixCell.CellContents = new CellContents();
    tablixCell.CellContents.ReportItem = CreateTextBox(textBoxName, textBoxValue);
    return tablixCell;
}

BoldReports.RDL.DOM.TablixMember CreateTablixMember(string groupName)
{
    var tablixMember = new TablixMember();
    tablixMember.Group = new Group();
    tablixMember.Group.Name = groupName;

    return tablixMember;
}

internal class TablixRowValues
{
    public string TextBoxName { get; set; }
    public string TextBoxValue { get; set; }
}

Create a Chart for the report

  • Using the following code snippets, you can create a Chart report item and assign the values for their properties like name, styles, and dimension values.
BoldReports.RDL.DOM.Chart   CreateChart(string name, string left, string top, string width, string height)
{
    var chartItem = new Chart();
    chartItem.Name = name;
    chartItem.Height = new BoldReports.RDL.DOM.Size("10in");
    chartItem.Width = new BoldReports.RDL.DOM.Size("10in");
    chartItem.Left = new BoldReports.RDL.DOM.Size("5in");
    chartItem.Top = new BoldReports.RDL.DOM.Size("5in");
    chartItem.DataSetName = "DataSet1";
    chartItem.Style = new BoldReports.RDL.DOM.Style();
    chartItem.Bookmark= "=First(Fields!Name.Value, \"DataSet1\")";

    chartItem.ChartCategoryHierarchy = new ChartCategoryHierarchy();
    chartItem.ChartCategoryHierarchy.ChartMembers = new ChartMembers();
    chartItem.ChartCategoryHierarchy.ChartMembers.Add(ChartCategoryMember());
    chartItem.ChartSeriesHierarchy = new ChartSeriesHierarchy();
    chartItem.ChartSeriesHierarchy.ChartMembers = new ChartMembers();
    chartItem.ChartSeriesHierarchy.ChartMembers.Add(ChartSeriesMember());
    chartItem.ChartData = new ChartData();
    chartItem.ChartData.ChartDerivedSeriesCollection = new ChartDerivedSeriesCollection();
    chartItem.ChartData.ChartSeriesCollection = CreateChartSeriesCollection();
    chartItem.ChartLegends = new ChartLegends();
    chartItem.ChartAreas = new ChartAreas();
    chartItem.ChartAreas.Add(CreateChartArea());
    chartItem.ChartTitles = new ChartTitles();
    chartItem.Palette = "Pacific";
    chartItem.ChartBorderSkin = new ChartBorderSkin();
    chartItem.ChartNoDataMessage = new ChartNoDataMessage();
    chartItem.ChartNoDataMessage.Caption = "No Data Available";
    chartItem.ChartNoDataMessage.Name = "NoDataMessage";

    return chartItem;
}

BoldReports.RDL.DOM.ChartMember ChartCategoryMember()
{
    var ChartMember = new ChartMember();
    ChartMember.ChartMembers = new ChartMembers();
    ChartMember.CustomProperties = new CustomProperties();
    ChartMember.DataElementName = null;
    ChartMember.DataElementOutput = DataElementOutputs.Auto;
    ChartMember.Group = new Group();
    ChartMember.Group = CreateChartGroup();
    ChartMember.Label="=Fields!DepartmentID.Value";
    ChartMember.SortExpressions = SortExpressions();

    return ChartMember;
}
BoldReports.RDL.DOM.ChartMember ChartSeriesMember()
{
    var ChartMember = new ChartMember();
    ChartMember.ChartMembers = new ChartMembers();
    ChartMember.CustomProperties = new CustomProperties();
    ChartMember.DataElementName = null;
    ChartMember.DataElementOutput = DataElementOutputs.Auto;
    ChartMember.Group = null;
    ChartMember.Label = "Department ID";
    ChartMember.SortExpressions = new SortExpressions();
    return ChartMember;
}

BoldReports.RDL.DOM.SortExpressions SortExpressions()
{
    var SortExpressions = new SortExpressions();
    var SortExpression = new SortExpression();
    SortExpression.Direction = SortDirection.Ascending;
    SortExpression.Value = "=Fields!DepartmentID.Value";
    SortExpressions.Add(SortExpression);

    return SortExpressions;
}
BoldReports.RDL.DOM.Group CreateChartGroup()
{
    var ChartGroup = new Group();
    ChartGroup.DataElementName = null;
    ChartGroup.DataElementOutput = DataElementOutputs.Auto;
    ChartGroup.DocumentMapLabel = null;
    ChartGroup.DocumentMapLabelLocID = null;
    ChartGroup.DomainScope = null;
    ChartGroup.Filters = new Filters();
    ChartGroup.GroupExpressions = CreateGroupExpressions();
    ChartGroup.Name = "Chart2_CategoryGroup";
    ChartGroup.PageBreak = null;
    ChartGroup.PageName = null;
    ChartGroup.Parent = null;
    ChartGroup.Variables = new Variables();

    return ChartGroup;
}

BoldReports.RDL.DOM.GroupExpressions CreateGroupExpressions()
{
    var GroupExpressions = new GroupExpressions();
    var GroupExpression = new GroupExpression();
    GroupExpression.Value = "=Fields!DepartmentID.Value";
    GroupExpressions.Add(GroupExpression);

    return GroupExpressions;
}

BoldReports.RDL.DOM.ChartSeriesCollection CreateChartSeriesCollection()
{
    var ChartSeriesCollection = new ChartSeriesCollection();
    ChartSeriesCollection.Add(CreateChartSeries());

    return ChartSeriesCollection;
}

BoldReports.RDL.DOM.ChartSeries CreateChartSeries()
{
    var ChartSeries = new ChartSeries();
    ChartSeries.CategoryAxisName = "Primary";
    ChartSeries.ChartAreaName = null;
    ChartSeries.ChartDataLabel = null;
    ChartSeries.ChartDataPoints = new ChartDataPoints();
    ChartSeries.ChartDataPoints.Add(ChartDataPoint());
    ChartSeries.ChartEmptyPoints = new ChartEmptyPoints();
    ChartSeries.ChartEmptyPoints.ChartDataLabel = new ChartDataLabel();
    ChartSeries.ChartEmptyPoints.ChartDataLabel.Style = new Style();
    ChartSeries.ChartEmptyPoints.ChartMarker = new ChartMarker();
    ChartSeries.ChartEmptyPoints.ChartMarker.Style = new Style();

    ChartSeries.ChartSmartLabel = new ChartSmartLabel();
    ChartSeries.Name = "DepartmentID";
    ChartSeries.Type = VisualizationType.Column;
    ChartSeries.Subtype = VisualizationSubType.Plain;
    ChartSeries.Style = new Style();

    return ChartSeries;
}
BoldReports.RDL.DOM.ChartDataPoint ChartDataPoint()
{
    var ChartDataPoint = new ChartDataPoint();
    ChartDataPoint.ActionInfo = null;
    ChartDataPoint.ChartDataLabel = null;
    ChartDataPoint.ChartDataLabel = ChartDataLabel();
    ChartDataPoint.ChartDataPointValues = new ChartDataPointValues();
    ChartDataPoint.ChartDataPointValues.Y = "=Sum(Fields!DepartmentID.Value)";

    ChartDataPoint.ChartItemInLegend = null;
    ChartDataPoint.ChartMarker = new ChartMarker();
    ChartDataPoint.ChartMarker.Size = null;
    ChartDataPoint.ChartMarker.Type = null;
    ChartDataPoint.ChartMarker.Style= chartStyle("Transparent", "Arial");
    ChartDataPoint.CustomProperties = new CustomProperties();
    ChartDataPoint.DataElementName = null;
    ChartDataPoint.DataElementOutput = DataElementOutputs.Output;
    ChartDataPoint.ToolTip = null;
    ChartDataPoint.Style= chartStyle("Transparent", "Arial");

    return ChartDataPoint;
}

BoldReports.RDL.DOM.ChartDataLabel ChartDataLabel()
{
    var ChartDataLabel = new ChartDataLabel();
    ChartDataLabel.ActionInfo = null;
    ChartDataLabel.Label = null;
    ChartDataLabel.Position = null;
    ChartDataLabel.Rotation = "0";
    ChartDataLabel.Style = new Style();
    ChartDataLabel.Style = chartStyle("Transparent", "Arial");
    ChartDataLabel.ToolTip = null;
    return ChartDataLabel;

}

BoldReports.RDL.DOM.Style chartStyle(string BackgroundColor, string FontFamily)
{
    var style = new Style();
    style.BackgroundColor = BackgroundColor;
    style.FontFamily = FontFamily;

    return style;
}

BoldReports.RDL.DOM.ChartArea CreateChartArea()
{
    var chartArea = new ChartArea();
    chartArea.Style = new Style();
    chartArea.Style = chartStyle("Transparent", "Arial");
    chartArea.AlignOrientation = AlignOrientation.None;
    chartArea.AlignWithChartArea = null;
    chartArea.ChartAlignType = null;
    chartArea.ChartElementPosition = null;
    chartArea.ChartInnerPlotPosition = null;
    chartArea.ChartThreeDProperties = null;
    chartArea.Hidden = false;
    chartArea.EquallySizedAxesFont = false;
    chartArea.Name = "Default";
    chartArea.ChartCategoryAxes = new ChartCategoryAxes();
    chartArea.ChartCategoryAxes.Add(createChartAxis("Primary"));
    chartArea.ChartCategoryAxes.Add(createChartAxis("Secondary"));
    chartArea.ChartValueAxes = new ChartValueAxes();
    chartArea.ChartValueAxes.Add(createChartAxis("Primary"));
    chartArea.ChartValueAxes.Add(createChartAxis("Secondary"));

    return chartArea;
}

BoldReports.RDL.DOM.ChartAxis createChartAxis(string Name)
{
    var ChartAxis = new ChartAxis();
    ChartAxis.Style = new Style();
    ChartAxis.AllowLabelRotation = AllowLabelRotation.None;
    ChartAxis.Angle = "0";
    ChartAxis.Arrows = Arrows.None;
    ChartAxis.ChartAxisScaleBreak = new ChartAxisScaleBreak();
    ChartAxis.ChartAxisTitle = new ChartAxisTitle();
    ChartAxis.ChartMajorGridLines = new ChartMajorGridLines();
    ChartAxis.ChartMajorTickMarks = new ChartMajorTickMarks();
    ChartAxis.ChartMinorGridLines = new ChartMinorGridLines();
    ChartAxis.ChartMinorTickMarks = new ChartMinorTickMarks();
    ChartAxis.ChartStripLines = new ChartStripLines();
    ChartAxis.CrossAt = "NaN";
    ChartAxis.CustomProperties = new CustomProperties();
    ChartAxis.LineStyle = LineStyle.Solid;
    ChartAxis.Name = Name;

    return ChartAxis;
}

Render the generated report in ReportViewer

You can render the report using the ReportViewer, after the report definition is created. The following code snippet illustrates how to render the report using the ReportViewer by report definition.

[NonAction]
public void OnInitReportOptions(ReportViewerOptions reportOption)
{
 var report = CreateReport();
 reportOption.ReportModel.ReportDefinition = reportDefinition;
}