[. Net] rdlc cyclically processes data

Source: Internet
Author: User

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 .)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.