ReportViewer DataSource and Expression Walkthrough

There is a problem to many report framework except JasperReport. The field expression syntax and their ability.
ReportViewer is not exception.
I have a post about some basic on using ReportViewer. But I recently found that a little part about binding data source is missing. That is the reason why this post is writen.
The post before is about object data source in code behind. There is one thing I forgot myself too. If you are wordering why your method do not appear in the data source window. A method that returns DataTable can not be a data source. You have to stick to Enumerable data. And in the code behind, you have to go to design view first, see what name of the data source should be named. You will need that to assign the data source to the ReportViewer.
我之前有一篇關於ReportViewer的基本應用. 但我最近發現中間有一小段關於連結資料源的地方漏掉了.這就是為甚麼我要寫這一篇的原因.
前一篇提到怎樣在code behind連結物件資料源.有一件事我自己也忘記了.如果你搞不懂為甚麼你的資料源取得方法沒有出現在資料源窗格.因為一個回傳DataTable的方法並不能作為一個資料源.你非得要用可列舉資料才行.至於code behind,你應該先去設計檢視,看一下資料源設定了甚麼名稱.在你指定資料源時要用到.
After data source are bound. You may want to do some extra checking in your expressions. Like show only positive number.
According to ReportViewer, it tells you that the expression is in VB syntax, I doublt it. You can’t even use If Then Else.
Here is the Link of MSDN on expression syntax.
If you want to check if a number is NaA, you can’t… The double in RDLC don’t have NaN property.
I just check it if it is larger than zero since I am removing negative number anyway.
在資料連結後.你可能會想給你的內容表達式加一點檢查.例如只顯示正數.根據ReportViewer的說明,它說所使用的是VB語法,我很懷疑.你連If Then Else都不能用.這是MSDN有關於表達式語法的文章.如果你想檢查一個數字是否NaN,沒辦法…因為RDLC裡的double並沒有NaN這個屬性.我只是檢查數值是否大於零,反正我都要忽略負數.
The syntax should be like

Iif(ExpressionOfField > 0, ExpressionOfField, "")

There is also an issue when you are using ReportViewer on IIS 7. You may encounter JavaScript error and your image buttons turn into ‘X’. That is because the HttpHandler registration is moved from System.Web to System.WebServer.
如果你在IIS 7上運行你的ReportViewer. 你可能會遇到JavaScript錯誤並發現你的小圖示都變成一個’X’.那是因為HttpHandler的註冊從System.Web移到System.WebServer了.
You can solve it by.

Open Internet Information Services (IIS) Manager and select your Web application.
Under IIS area, double-click on Handler Mappings icon.
At the Action pane on your right, click on Add Managed Handler.
At the Add Managed Handler dialog, enter the following:
Request path: Reserved.ReportViewerWebControl.axd
Type: Microsoft.Reporting.WebForms.HttpHandler
Name: Reserved-ReportViewerWebControl-axd
Click OK.

The solution was found here, thanks to Antonio Ooi. I hope he don’t mind me quoting his article.
解決方法來自這裡,感謝Antonio Ooi.希望他不介意我引述其文章.


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s