Let's remember a fairly common situation when you need to do something very quickly and using whatever is available. But let's not forget that the structure and content of the report depend on external factors. The FastReport.NET report generator is a very flexible product and gives you two ways to solve this problem.
Method 1. To create a report structure in the code of a custom application.
Method 2. To control the behavior of the report inside, using a script.
There is quite a lot of information on how to implement certain behavior features in a report script. But quite a few examples show how to create a report from code. In this article, we will create a report with a table from the application code, and we’ll also look at how to fill a table with data and place an object in a table cell. Method 2 will be described in more detail in another article.
We can fill the Table object with static or dynamic data. In the first case, we know the specific dimensions of the table and enter the data into the cells from the fixed set straight away.
In the second case, the table is created dynamically, rows (columns) are added depending on the data in the source. However, you can also limit the size of the table and fix it.
Let's create a WinForms application. Let's include the FastReport.dll library in the links, which you can find in the folder with the installed report generator. Let's add a button to the form that will start building the report. Don't forget to create for it a click handler.
First of all, we include the FastReport libraries.
using FastReport; using FastReport.Data; using FastReport.Table; using FastReport.Utils; private void button1_Click(object sender, EventArgs e) { using (Report report = new Report()) // Create a report object { ReportPage page = new ReportPage(); //Create a report page object page.Name = "Page1"; //Set the name of the page report.Pages.Add(page); //Add a page to the collection of report pages DataSet ds = new DataSet(); //Create a data source ds.ReadXml("~/../../../App_Data/nwind.xml"); //Load the xml database into it report.RegisterData(ds); //Register the data source in the report report.GetDataSource("Products").Enabled = true; //Enable the data source DataBand dataBand = new DataBand(); //Create a data band dataBand.Name = "DataBand"; //Specify the band name page.Bands.Add(dataBand); // Add a band to the page's band collection TableObject table = new TableObject(); //Create a table object table.Name = "Table1"; // Specify the name of the object table.RowCount = 10; // Specify the number of rows table.ColumnCount = 2; //Specify the number of columns //Fill all the cells with some data in the loop for (int i = 0; i < 10; i++) for (int j = 0; j < 2; j++) { table[j, i].Text = (10 * i + j + 1).ToString(); table[j, i].Border.Lines = BorderLines.All; } dataBand.Objects.Add(table); //dataBand.Objects.Add(picture); if (report.Prepare()) report.ShowPrepared(); }
Now let's look at the example when we need to fill the table with data from the source. Let's replace the loop above with another code:
table.Columns[0].AutoSize = true; //table.Columns[1].AutoSize = true; DataSourceBase data = report.GetDataSource("Products"); data.Init(); //Let’s initialize the data source data.First(); //We get the first record for (int i = 0; i < 10; i++) { table[0, i].Text = data["ProductName"].ToString(); table[0, i].Border.Lines = BorderLines.All; //Let’s set the borderlines table[1, i].Text = data["UnitPrice"].ToString(); table[1, i].Border.Lines = BorderLines.All; data.Next(); }
In the end, we will get a table with data from the database:
The table looks incomplete without a title. Let's add it:
table.RowCount = 11; … table[0, 0].Text ="Product Name"; table[0, 0].Border.Lines = BorderLines.All; table[1, 0].Text = "Unit Price"; table[1, 0].Border.Lines = BorderLines.All; for (int i = 1; i < 10; i++) { table[0, i].Text = data["ProductName"].ToString(); table[0, i].Border.Lines = BorderLines.All; table[1, i].Text = data["UnitPrice"].ToString(); table[1, i].Border.Lines = BorderLines.All; data.Next(); }
Let me clarify that we specified the titles in the first row of the table, which means that the cycle begins not from the first, but the second element.
Let's look at the last case for today - how to place an object in a table cell. For example, a picture:
PictureObject picture = new PictureObject(); //Create a picture object picture.Bounds = new RectangleF(40, 0, Units.Centimeters * 0.5f, Units.Centimeters * 0.5f);
// Set the size of the object picture.CreateUniqueName(); //Set an arbitrary name picture.Image = Image.FromFile("C:/Users/FR/Downloads/28.png"); //Specify the path to the image picture.LoadImage(); //Load the image picture.Parent = table[1, 1]; //Specify the parent object for the image
It is the Parent property that affects the way the picture will be displayed in the cell. Let's see how it will look:
Thus, we’ve looked at how to create a table in the report from the application code and two ways to fill it with static or dynamic data. In addition, now you know how to programmatically add an object to a table cell.