When using rdlc to develop reports, if the data source content is as follows.
private List<UserData> CreateDataSet(){ List<UserData> userDataSet = new List<UserData>(); userDataSet.Add(new UserData() { Name = "Clark", Age = 18 }); userDataSet.Add(new UserData() { Name = "Yaya", Age = 15 }); return userDataSet;}
We can quickly use the data table control in rdlc to present data in reports.
However, when the customer requires a report, the data source content should be displayed in one row. Rdlc processing is not that simple, because rdlc does not have the function of loop processing. The developer must use a hidden list and an external string to combine data... And so on. However, there are no efficiency and many restrictions for such development.
This problem has plagued me for several days. I slept halfway last night and suddenly got inspiration. You can use the sum function in rdlc to process data loops.
The sum function retrieves each piece of data in the dataset and calculates the sum of all data. We can write the expression as the following function, and use the code. pushstringstack rdlc custom function to let the sum function get all the data. In this way, each piece of data in the dataset can be handed over to code. pushstring for processing once.
=Sum(code.PushString(Fields!Name.Value, Fields!Age.Value), "UserDataSet")
Next, let's look at the rdlc custom function code. pushstring, which uses a static string variable to record the results after each data processing. Developers can understand that this method is to process data cyclically.
Public Shared _foreachResult As String = String.EmptyPublic Shared Function PushString(userName As String, userAge As String) As Integer _foreachResult += userName & "(" & userAge & "), " Return 0End Function
Of course, after processing the data in a loop, you still need to display the data on the screen. A code. popstring callback is created.
Public Shared Function PopString() As String Return _foreachResultEnd Function
And rewrite the original expression:
=IIF(Sum(code.PushString(Fields!Name.Value, Fields!Age.Value), "UserDataSet") <> 0, "", code.PopString())
After compilation and execution, you can see that the data format required by the customer is correctly displayed in the rdlc text box.
Sample program: rdlcforeachdatasample click here to download.
(Simplified Design for convenience of illustration. Developers should establish implementation based on actual scenarios .)