Repeat a RDLC ReportViewer subreport - c#

I'm really new to RDLC so please forgive me if I'm missing something obvious.
I have some data that needs to get repeated, based on a 1:Many dataset that's linked to the main report. It's not something that can be displayed in a tabular fashion because the customers want it laid out in a specific manner, so using the Tablix control is out. I have a number of subreports based on 1:1 datasets in this main report, and everything is cool with those. I created a subreport for this new dataset and added it to the main RDLC report. Everything works fine, but it only includes the first record of the dataset.
Is it possible to repeat this subreport for each record in the dataset? I think the thing that confuses me is the main RDLC has no code that specifically loads the individual subreports, so I don't see anyplace obvious where I could include a loop or anything.

If you don't have particular needs I think it's important to know that you can put a Rectangle in Tablix cell and then use it as a container for simple controls as TextBox, Line, Image, etc. laied out as you want.
If you still need to use subreport you could put a SubReport in Tablix cell and solve any problem in the LocalReport.SubreportProcessing event that occurs when a subreport is processed.
If you have a lot of records you can use a single Dataset and filtered it in the SubreportProcessing event, using the subreport Parameters that you already set in Designer Mode.
Private Sub SubreportProcessingHandler(sender As Object, e As SubreportProcessingEventArgs)
Dim dvFiltered As New DataView(Me.YourDataSet.Tables(0))
dvFiltered.RowFilter = "Parameter1 = " & CInt(e.Parameters.Item("yourParameter1").Values(0)) & " AND " _
& "Parameter2 = '" & CStr(e.Parameters.Item("yourParameter2").Values(0)) & "'"
e.DataSources.Add(New ReportDataSource("YourDataSourceName", dvFiltered.ToTable("YourDataSourceName")))
End Sub
For example, using a DataSet that contains master and details data, you can build a main report grouped by IdMaster and put a subreport in details section.
This is the subreport: please note that the DataSet is the same as main report but we also need 2 Parameters (IdMaster and IdRow) to display the correct data.
In the main report you need to link subreport Parameters to actual values of DataSet.
Then, the most important part: the SubreportProcessingHandler event. This event is triggered for every instance of the subreport in the main report, so if you have 100 rows / 100 subreports this event is triggedered 100 times: every time you need to specify which data you want to display, so you have to filter the DataSet using the 2 Parameters (IdMaster and IdRow) defined in the previous step and filled with values coming from master report.
Private Sub SubreportProcessingHandler(sender As Object, e As SubreportProcessingEventArgs)
Dim dvTest As New DataView(Me.dsTest.Tables(0))
dvTest.RowFilter = "IdMaster = " & CInt(e.Parameters.Item("parIdMaster").Values(0)) & " AND " _
& "IdRow = " & CInt(e.Parameters.Item("parIdRow").Values(0))
e.DataSources.Add(New ReportDataSource("DataSet_TEST", dvTest.ToTable("DataSet_TEST")))
End Sub
This is the result:
As I stated at the beginning of the answer, if you don't have particular needs you can use a Rectangle instead of a SubReport. Regarding this example you can obtain the same result using the green Rectangle as container.

Related

How to load two RDLC reports in same report viewer?

I have two reports the first one to show general information and the second one to show details and I need to show each one in a different page in report viewer
can you help me to do that
thanks
like the image
Hopefully, the following can help guide you into what you are looking for. First, you will need THREE reports and TWO datatables (or other lists as you have to build your report).
First, On the 3 reports I describe, I would not worry about any of the report final formatting to be perfectly aligned, bold, spacing... get it to work, then you can make it pretty after all is functional.
Next, create a bogus datatable "MyTable" for the sole purpose of creating two records. Even if a single column of "MyGroup" and having it as an int (just for sample purposes).
Now, create two records and set the values in each row to 1 and 2 respectively so your table has
MyGroup
1
2
Now, for the other two reports. It is my understanding that you want the first report to show a summary of each department's totals with a grand-total. The second report is showing the details of all the activity per department. So this is basically using the same dataset returned from whatever query. For this example, I will refer to the datatable as "SalesData".
CREATING THE REPORTS
Create your first report, put a table control on the report, full width you will have for your current reports as landscape or portrait as long as both the same size, you want this report to be the same. Ex: ALL will be portrait. Add your Report Data source to show the "MyTable" and assign that to the table control. Also add the "SalesData" table to the report as well so it is available to the future sub-reports.
On the table control, below where it shows the "Row Groups", add a "Group By" for the "MyGroup" column. This is so we can force a page-break between each group. Once added, right-click on the parent group "MyGroup" and select "Group Properties". This will show an option on the left side to allow you to set page breaks, click the checkbox for "Between each instance of a group". For now, just have the one column in the table control as the "MyGroup" column. Save the report ex: "MySalesRpt.rdlc". Then compile and run the report. You should get this simple report with 2 pages, each showing just the number 1 and 2 respectively. NOTE: You will also want to add the datatable reference for your secondary reports to this main report as well so they are available as sub-reports of the first.
Create a new report ex: "MySalesDetail.rdlc", add the "SalesData" to it. Add a table control to the report and set its source to the "SalesData" table. Include whatever for your detail columns you want to show of the sales activity. Add a data group based on the department, get your sum( salesDollarColumn ). Add a new row outside/below the group footer so you can have the report grand totals as a sum of all ex: SalesDollarColumn. You can test and run this report on its own until it looks correct.
Now, your third report. Copy/paste the MySalesDetail.rdlc report and change it to MySalesSummary.rdlc. So you don't this summary report showing every row that builds the grand total sum() per each department, right-click on the row that represents the details of the group and delete. It will ask if you want to delete rows and associated groups? Reply yes as it will just remove the details row, but keep the group footer that will retain your sum() total per department. Save, test / run this report.
Once all 3 reports work individually, now we can go back to the "MySalesRpt". Add an additional DETAIL row in this otherwise simple 1-detail report. Right-click on the existing detail row and then pick "Insert Row" -> "Inside Group - Below". This will now show as two rows.
Now, in the first row of the report where it did have just a simple textbox, go to your report Toolbox and pick Subreport and put one instance into each row.
On the first row, subreport, right-click and set properties. Set both the Name and "Use this report as a subreport" to the summary report "MySalesSummary". On the second row subreport, do the same but with "MySalesDetail".
Final step. Click on the Subreport representing the "MySalesSummary". Go to the properties of that and scroll down to the "Hidden" property. You want to have this value set to the expression: ex: "=Fields!MyGroup.Value = 2". This way the report will ONLY be generate when it is on the first record where MyGroup = 1.
Similarly, on the second subreport representing the detail, set the hidden equal to the opposite.. "=Fields!MyGroup.Value = 1". Hide when the MyGroup = 1. This way you can have each report on their own page, but only one will show up per group.
I know is sounds deep, but do it slowly and it should work for you. I already did and confirmed this process works. Sorry so many steps, but it has to be broken into multiple parts to tie them all together as you are requesting.
One rdlc in main local report and the other in subreport. Then set InteractiveSize in the main local report to break it to show each one in a different page in report viewer.

How to generate product receipt in winform application?

I am working on a winform application for the first time and I have a gridview which contains a list of products users have bought.
I have a Print button on click which allows the user to generate a receipt like the one below:
So here I am confused whether I should use "winform default RDLC or Crystal Report" or whether I should generate PDF and then let it print out as receipt, but I am not sure if PDF is a good option for receipt generation or not.
For Crystal Report, I have read that I need to install it and client (who will use this desktop application) had to install Crystal Report and also there is some licensing involve with Crystal Report which I don't want.
Also if I use Crystal Report then I am not sure if it would be possible to generate exactly above receipt (with table formatting) and will it be complicated?
Receipt is bit complicated so is there a better tool or way, or how should I generate receipt I have shown in above image?
Update : Printing paper total size is : 7.50 centimeter and user wants to print all the content in center.
Discount = FinalAmount - MRP;
Customer Name, Mobile No, Bill No, Payment Mode values are entered on the form by user itself.
I am having a Excel file which contains list of products and with each products I have information like ProductId,ProductName,MRP,Tax information like CGST,SGST.
Code to fill gridview from excel file based on Product Id:
using (OleDbConnection cnnxls = new OleDbConnection(strConn))
using (OleDbDataAdapter oda = new OleDbDataAdapter(query, cnnxls))
{
oda.Fill(dtProductList);
DataColumnCollection columns = dtProductList.Columns;
if (!columns.Contains("FinalAmount"))
{
dtProductList.Columns.Add(new DataColumn() { ColumnName = "FinalAmount", DataType = typeof(decimal) });
}
if (!columns.Contains("Quantity"))
{
dtProductList.Columns.Add(new DataColumn() { ColumnName = "Quantity", DataType = typeof(int) });
}
DataRow lastRow = dtProductList.Rows[dtProductList.Rows.Count - 1];
lastRow["FinalAmount"] = Convert.ToDecimal(lastRow["MRP"]);
lastRow["Quantity"] = 1;
}
Generate and print the receipts
You can use any report designer tool like RDLC Reports or Crystal Reports to generate a report. RDLC reports are good enough. You can print the RDLC report with or without showing the print dialog. You can also easily export the RDLC report manually or using the code.
If for any reason you don't want to use a reporting tool, as another option you can consider generating HTML report easily using Run-time T4 templates.
Using an RDLC report, how to show multiple fields in a single cell
You can easily use an expression to show multiple values in a single cell. Also as another option, you can use rows in a single row group and show different fields in a single column.
Example 1 - RDLC - Show multiple fields in a single column using expression
The following steps show you how you can display multiple fields in a single column using expression. I assume you have set up the data source and have ProductName, UnitPrice and Quantity fields. Then, follow these steps:
Drop a Table from toolbox on the report design surface.
In first column, first data row (not the header row), right click and choose ProductName (image)
Select the header of the second column and type UnitPrice/Quantity (image)
In second column, first data row, right click and choose Expression. (image)
In the expression window, enter the desired expression, for example:
= "UnitPrice: " & Fields!UnitPrice.Value.ToString() & System.Environment.NewLine & "Quantitye: " & Fields!Quantity.Value.ToString()
Example 2 - RDLC - Show multiple fields in a single column using row group
The following steps show you how you can display multiple fields in a single column. I assume you have set up the data source and have ProductName, UnitPrice and Quantity fields. Then, follow these steps:
Drop a Table from toolbox on the report design surface.
In first column, first data row (not the header row), right click and choose ProductName (image)
Select the header of the second column and type UnitPrice/Quantity (image)
Right click on row header of the first data row and choose Insert Row → Inside Group - Below (image)
In second column, first data row, right click and choose UnitPrice. (image)
Click on the [UnitPrice], and then press Home and type UnitPrice: (image)
Do the same for Quantity, in the next row in the group.
If you need another row in the group, repeat step 3.
You can setup borders of the cells by selecting them and setting BorderStyle individually for top, left, bottom and right.
Download
You can clone or download an example using expression here:
repository
zip file
A quick and easy way I used before was to generate a html page, and then use the html2pdf library to convert it to a pdf file.
You may also consider this approach since the RDLC reports/Crystal reports may be a overkill for your case.
The RDLC is powerful as well as Crystal reports. You may choose the rdlc which comes close in eliminating licensing costs.
Using RDLC
Data
You need to add datasets Here or data sources to the report which you will manipulate to meet the design and data you want.
Design
On design you just drag and drop controls to your taste. There is a challenge that sometimes what you see on the design may not what be you see on final output so you need to test much.
Printing
You can put a print preview or send directly to a pdf viewer using rdlc. Here is an example.
Conclusion
I think If you have your data generated well on the report, the design and layout won't be much of a problem using both rdlc and crystal reports.
UPDATE
Based on further information provided I have tried to do something that may come close to what you want to achieve.
I have used crystal reports as well as database table to simulate because of time. Otherwise the same can be achieved using rdlc.
The sample table i created
Here is the sample query and results from the database. I have made groups that can be accomodated by the crystal reports. You can do calculated text values using the same to put distinction between the Tax information as well as Transaction Memo.
Here is the final look after tweaking the design. The page layout may also be tweaked with regards to your taste.
Update.
For RDLC I think you need to add datasets for memo data and tax information. Take a look at the below if it comes close. I failed to make a preview there were components I hadn't installed.
for adding 3 columns in one cell
you have two options:
1- Use new line expression
=Fields!MyField1.Value + System.Environment.NewLine + Fields!MyField2.Value
2- Use something like subreport or grouping in rdlc.
the first option seams easier

crystal report move table to next page

I am using c# crystal report,i have some text in header section below comes is a table in sub-report below table is footer section,if table rows grows in size,end rows get cutoff,what i want to do is if table rows grew in size it should get display in next page and below that footer section should be display.
Do anyone knows how to achieved it.
Thanks in advance.
First you need to give Can Grow option for the fields inside the sub report and also
give the same option for the sub report too
FYI, Can Grow option is available in the context menu which can be accessed by right clicking on the sub report and choose the format report option

How to filter data in RDLC report?

I am new to RDLC reports in Visual Studio 2010.
I am working on a Website, in this I have used report feature of VS2010. I use ReportViewer to show Report1.rdlc. It is showing it perfectly. Now my requirement is to filter data in report at runtime. I go through this question but not able to understand it's answer.
Because Report1.rdlc is not available in code behind.
Now, how can I filter report's data at runtime?
Surely you assign the dataset to the rdlc report, and in the page of your reportviewer, you would be using an object datasource (and therefor an XSD dataset with datatables), which encapsulates the query.
The query naturally has a where clause, in which you insert your parameters to filter the data by, like:
WHERE (Table.Field = #Param OR #Param = A_Default_Value)
Now add the controls you like to the page before calling the report (drop-down lists, textboxes, etc..), those controls filter the Object datasource, which is mapped to a dataset in the rdlc report, and thus, the report would be filterd upon clicking on a certain button, e.g. Show Report, to refresh the object datasources (databinding them) and re-showing the report in the reportviewer.
You may consider assiging the values of the filtering controls to parameters in the rdlc, so that you may show the parameters of the report in its header upon generating it:
Parameters!ParameterName.Value
Hope that helps you.
How are you getting the data from Server, etc. Are you binding to a datatable result? If so, you can just have your query apply the filter when extracting data to begin with so you don't have to do anything special in the actual report.
Per your feedback, if you are getting a DataTable, then applying a filter should be easy like
DataTable oDT = YourSQLCommandToGetData();
oDT.DefaultView.RowFilter = "SomeColumn = x and OtherColumn = y";
And then run your report. If you want to take it to another level and ENSURE you don't have any unwanted records, you could even do something like
DataTable oNewFilteredResult = oDT.DefaultView.ToTable();
Then, you can assign the NEW table to call the RDLC.

Using subreport inside an SSRS table/tablix cell

In my application I have a report with one subreport contained inside a table cell. Within the SubreportProcessing event handler I supply a different set of data foreach instance of the subreport. In VS 2008 this worked okay. However, when I switched over to VS2010 and upgraded the report file format, the behavior changed. All subreport instances in the master table now contain the data that I supplied for the first table row. My code looks like this
void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
{
// _index is a global variable that is reset to zero in the DataBind procedure.
Trip currentTrip = _trips[_index];
e.DataSources.Add(new ReportDataSource("DataSourceName", currentTrip.Items));
_index++;
}
Is this the intended behavior? How can I now supply different datasets for multiple instances of the same subreport inside a table?
Thanks in advance.
Vladislav
After some poking around and tinkering with my code, I found a workaround. The solution is to create a dummy parameter in the subreport, which you then need to bind to a field in the tablix dataset. Any field will do as long as both the subreport parameter and the tablix dataset field are type-compatible. You don't have to do anything with the parameter in the subreport, but now SSRS displays multiple instances of the same subreport, a separate one for each row in the tablix.
Hope this will be helpful for someone else, too.
Vladislav

Categories