2017年9月7日 星期四

RDLC 固定筆數分頁(一)

針對需要設定每頁只列印固定筆數就換頁的需求

環境

    Visual Studio 2017 Community
    Microsoft.ReportViewer.WinForm 版本是 14.0.0.0

示範用的 RDLC 使用的資料集的格式

public class FixedRowEachPageDataSet
{
    public string ItemCode { get; set; }
    public string ItemName { get; set; }
    public string ItemPrice { get; set; }
    public string ItemStock { get; set; }
}

怎麼建立 RDLC 頁面的部份不做說明

首先在 RDLC 中建立好資料表,並綁定資料集


在詳細資料上點右鍵,選加入群組 > 父群組,顯示 Tablix 視窗



在 Tablix 群組視窗上輸入公式,其中的數字即是每頁要顯示的頁數

    =Ceiling(RowNumber(nothing)/15)


接著在建立的Group1群組上點右鍵選群組屬性


在「分頁符號」頁籤勾選分頁方式


在「排序頁籤」刪除裡面的排序依據
因為排序運算式會預設成群組運算式的內容,但是排序運算式不能用RowNumber,否則會報錯


設定標頭重覆
在完成上面步驟之後,目前畫面上應該像這樣,點右邊的小箭頭,選「進階模式」


選代表 Group 的那個靜態,如果把自動生成的那個 Group1 欄刪掉了話,就是點另一個(靜態),點下去之後按 F4 叫出屬性視窗,並修改屬性中的值,即完成標頭重覆

    KeepWithGroup = After
    RepeatOnNewPage = True


2017年9月4日 星期一

ASP.NET MVC DisplayTemplates

範本檔案放置的路徑是固定的

    /Views/ControllerName/DisplayTemplates
    /Views/Shared/DisplayTemplates

檔名需要與物件的型別相同,即若物件型別為 DateTime,則檔名需為 DateTime,或在
Model 上以 [UIHint("YourDisplayTemplatesName")] 標籤指定顯示範本的檔名

選定檢視範本的方式有三種,其順序為
1.UIHine 屬性
2.DataType 屬性
3.物件的資料型別名稱(不含命名空間)

蠻適合拿來處理西元轉民國年問題
在處理檢示範本頁面上需要注意會不會傳入的值是 null 的問題

實作方式

在 /Views/Shared/DisplayTemplates 中加入 twDateTime.cshtml
可能需要自行建立 DisplayTemplates 資料夾
這頁就是檢視範本,最後輸出什麼到 View 上就是看這頁最後會產出什麼

  1. @model DateTime
  2. @using System.Globalization
  3. @{
  4. TaiwanCalendar tc = new TaiwanCalendar();
  5. }
  6. @tc.GetYear(Model)/@tc.GetMonth(Model)/@tc.GetDayOfMonth(Model)

在 Model 中加入 DTTest.cs

  1. public class DTTest
  2. {
  3. [UIHint("twDateTime")]
  4. [DataType(DataType.DateTime)]
  5. public DateTime Dt { get; set; }
  6. }

在 Controller 中加入

  1. public ActionResult ShowTWDt()
  2. {
  3. var DTTest = new DTTest()
  4. {
  5. Dt = DateTime.Now
  6. };
  7.  
  8. return View(DTTest);
  9. }

在 View 中加入 ShowTWDt.cshtml
    @model ControllerPratice.Models.DTTest
    @Html.DisplayFor(model => model.Dt)