Posted 25 April 2023, 6:35 am EST
- Updated 25 April 2023, 6:40 am EST
I think I must be missing a step on utilizing the dataset. I have a report that has a top section that consists of a bunch of text fields. It has a datasource (called MyDataSource) and dataset that is hooked up to a query. Then in code I am building a table and adding it to this report. This table will have display the data from the datatable dataset.
From a web page, a user will select a report to generate and it will export the report to a PDF without displaying the report in a viewer or anything. I have it working, except the data is not populating from the datatable dataset.
This is my code to create the table in the code. I create a table, then create my header row. Then I create the details row. The textbox or checkbox values in my detail row come across as “=Fields!Print.Value”, “=Fields!CompType.Value”, etc.
//Create a Table
Table table = new Table();
table.Name = "tblCompensationDetails";
//create header row
TableRow trHeader = new TableRow();
trHeader.Height = "1.25cm";
table.Height += "1.25cm";
table.Width = (totalWidth <= 8 ) ? "8in" : "11in";
TextBox tb = new TextBox();
TableCell tc = new TableCell();
TableColumn tcol = new TableColumn();
//Loop over header values to build header row
for (int i = 0; i < headerDetails.Count; i++)
{
//build a textbox holding the header string
HeaderProperty hp = properties.Where(p => p.Name == headerDetails[i].ColumnName).ToList()[0];
tb = new TextBox();
tb.Name = "txtHeader" + headerDetails[i].ColumnName.Replace(" ", "").Replace("-", "");
tb.Value = headerDetails[i].ColumnName;
tb.Style.PaddingBottom = tb.Style.PaddingLeft = tb.Style.PaddingRight = tb.Style.PaddingTop = ExpressionInfo.FromString("2pt");
tb.Style.TextAlign = ExpressionInfo.FromString("Left");
tb.Style.VerticalAlign = ExpressionInfo.FromString("Middle");
tb.Style.FontSize = ExpressionInfo.FromString("7pt");
tb.Style.FontWeight = ExpressionInfo.FromString(hp.FontWeight);
tb.Style.WrapMode = ExpressionInfo.FromString("WordWrap");
tc = new TableCell();
tc.ReportItems.Add(tb);
tc.ReportItems[0].Style.BackgroundColor = ExpressionInfo.FromString(_headerFooterColor);
tcol = new TableColumn();
tcol.Width = hp.Width;
tcol.FixedHeader = true;
table.TableColumns.Add(tcol);
trHeader.TableCells.Add(tc);
}
table.Header.TableRows.Add(trHeader);
//loop over 1 data row create the detail data row object for the table
TableRow trData = new TableRow();
foreach (MFSRecordDetailGrid obj in tableRowData[1])
{
//build a textbox holding text
HeaderProperty hp = properties.Where(p => p.Name == obj.ColumnName).ToList()[0];
tc = new TableCell();
switch (hp.ColumnType)
{
case "CheckBox":
GrapeCity.ActiveReports.PageReportModel.CheckBox cb = new GrapeCity.ActiveReports.PageReportModel.CheckBox();
cb.Name = "chk" + obj.ColumnName.Replace(" ", "").Replace("-","") + obj.RowNumber;
cb.Checked = ExpressionInfo.FromString("=Fields!" + obj.ColumnName.Replace(" ", "").Replace("-", "") +".Value");
cb.Style.PaddingBottom = cb.Style.PaddingLeft = cb.Style.PaddingRight = cb.Style.PaddingTop = ExpressionInfo.FromString("2pt");
//tb.Style.TextAlign = ExpressionInfo.FromString("Left");
cb.Style.VerticalAlign = ExpressionInfo.FromString("Middle");
cb.Style.FontSize = ExpressionInfo.FromString("7pt");
tc.ReportItems.Add(cb);
break;
case "TextBox":
tb = new TextBox();
tb.Name = "txt" + obj.ColumnName.Replace(" ", "").Replace("-", "") + obj.RowNumber;
tb.Value = "=Fields!" + obj.ColumnName.Replace(" ", "").Replace("-", "") + ".Value";
tb.Style.PaddingBottom = tb.Style.PaddingLeft = tb.Style.PaddingRight = tb.Style.PaddingTop = ExpressionInfo.FromString("2pt");
tb.Style.TextAlign = ExpressionInfo.FromString("Left");
tb.Style.VerticalAlign = ExpressionInfo.FromString("Middle");
tb.Style.FontSize = ExpressionInfo.FromString("7pt");
tb.Style.WrapMode = ExpressionInfo.FromString("WordWrap");
tb.Style.Color = ExpressionInfo.FromString((obj.IsProgramDefaultRow.GetValueOrDefault()) ? _programDefaultColor : "#000000");
tc.ReportItems.Add(tb);
break;
}
tc.ReportItems[0].Style.BackgroundColor = ExpressionInfo.FromString("=IIF(RowNumber() Mod 2 = 0, \"#E5E5E6\", \"#cececf\")");
trData.TableCells.Add(tc);
}
table.Details.TableRows.Add(trData);
//Position the table on the page - the top needs to be far enough down on the page it does not cover the top section of the report;
table.Top = (table.Width == "11in") ? "7cm" : "3.6in";
table.Left = "0.0in";
table.DataSetName = "CustomDataSet";
//add report to page
//pageReport.Report.Body.ReportItems.Add(table);
pageReport.Report.ReportSections[0].Body.ReportItems.Add(table);
//create a generic datasource and add fields to our report
CreateReportDataSource(pageReport, headerDetails);
BuildDataTable(tableRowData, headerDetails);
pageReport.Document.LocateDataSource += Document_LocateDataSource;
pageReport.Run();
// Provide settings for your rendering output.
GrapeCity.ActiveReports.Export.Pdf.Page.Settings pdfSetting = new GrapeCity.ActiveReports.Export.Pdf.Page.Settings();
// Set the rendering extension and render the report.
GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension pdfRenderingExtension = new
GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension();
System.IO.DirectoryInfo outputDirectory = new System.IO.DirectoryInfo(@"C:\MyPDF");
outputDirectory.Create();
GrapeCity.ActiveReports.Rendering.IO.FileStreamProvider outputProvider = new GrapeCity.ActiveReports.Rendering.IO.FileStreamProvider(outputDirectory,
System.IO.Path.GetFileNameWithoutExtension(fileName));
// Overwrite output file if it already exists
outputProvider.OverwriteOutputFile = true;
pageReport.Document.Render(pdfRenderingExtension, outputProvider, pdfSetting);
Here is my code using your information from this ticket to create my datatable dataset.
private void CreateReportDataSource(PageReport report, List<MFSRecordDetailGrid> headerDetails)
{
// Creating our report DataSource and choosing the DataProvider as Dataset provider
DataSource ds = new DataSource() { ConnectionProperties = new ConnectionProperties() { DataProvider = "DATASET" }, Name = "DataSource1" };
// Creating a dataset for our report
GrapeCity.ActiveReports.PageReportModel.DataSet dataSet = new GrapeCity.ActiveReports.PageReportModel.DataSet() { Name = "CustomDataSet", Query = new Query() { DataSourceName = "DataSource1" } };
// Adding all the required fields to the dataset to be able to use within report's controls.
foreach (MFSRecordDetailGrid item in headerDetails)
{
dataSet.Fields.Add(new Field() { Name = item.ColumnName.Replace(" ", "").Replace("-",""), DataField = item.ColumnName.Replace(" ", "").Replace("-", "") });
}
// Adding the DataSource and DataSet to our report.
report.Report.DataSources.Add(ds);
report.Report.DataSets.Add(dataSet);
}
private void BuildDataTable(Dictionary<int, List<MFSRecordDetailGrid>> tableRowData, List<MFSRecordDetailGrid> headerDetails)
{
// Create a new DataTable.
System.Data.DataTable dt = new System.Data.DataTable("CompensationDetails");
System.Data.DataColumn dtColumn;
foreach (MFSRecordDetailGrid item in headerDetails)
{
dtColumn = new System.Data.DataColumn();
switch (item.SavedValueType)
{
case "INT":
dtColumn.DataType = typeof(Int32);
break;
case "VARCHAR":
dtColumn.DataType = typeof(String);
break;
case "MONEY":
dtColumn.DataType = typeof(Decimal);
break;
}
dtColumn.ColumnName = item.ColumnName.Replace(" ", "").Replace("-", "");
dtColumn.Caption = item.ColumnName;
dtColumn.ReadOnly = true;
dtColumn.Unique = false;
dt.Columns.Add(dtColumn);
}
_ds.Tables.Add(dt);
for (int i = 1; i < tableRowData.Keys.Count; i++)
{
System.Data.DataRow newRow;
newRow = _ds.Tables[0].NewRow();
for (int n = 0; n < tableRowData[i].Count; n++)
{
switch (tableRowData[i][n].SavedValueType)
{
case "VARCHAR":
newRow[tableRowData[i][n].ColumnName.Replace(" ","").Replace("-", "")] = tableRowData[i][n].SavedValueVarChar;
break;
case "MONEY":
newRow[tableRowData[i][n].ColumnName.Replace(" ", "").Replace("-", "")] = tableRowData[i][n].SavedValueMoney;
break;
case "INT":
newRow[tableRowData[i][n].ColumnName.Replace(" ", "").Replace("-", "")] = (tableRowData[i][n].SavedValueInt == 1) ? true : false;
break;
}
}
_ds.Tables[0].Rows.Add(newRow);
}
}
void Document_LocateDataSource(object sender, GrapeCity.ActiveReports.LocateDataSourceEventArgs args)
{
args.Data = _ds;
}
My generated PDF looks like this even though there are 10 rows of data in the dataset.table[0].
The first screenshot shows my dataset’s fields. The second shows my report PDF. I had to blank out the top data except the date because it’s sensitive and the top part is populating appropriately from my first datasource that is connected to a Stored Procedure. The Header Row is populating, but the data rows are missing in my table.