FastReport教程:如何使列突出顯示,具體取決於列的值

下載FastReport.Net最新版本html

Matrix報表是分析數據的絕佳工具。實質上,分析報表中的矩陣是一個彙總表。「條件分配」一般用於促進分析。這是FastReport.Net中的常規工具。條件突出顯示意味着使用顏色,字體或圖標突出顯示數據單元格,具體取決於給定條件。可是,條件突出顯示適用於我的可是若是咱們想根據標題中的值選擇整個列呢?例如,要突出顯示週末。在這種狀況下,您將不得不訴諸報表的「無所不能」腳本,常規顏色突出顯示在這裏沒有幫助。數據庫

使用基於nwind.xml演示數據庫中的MatrixDemo表的矩陣建立模板:工具

咱們的想法是在標題中找到知足條件的值。在這個矩陣中,咱們按年度計算員工的收入。字體

讓咱們突出顯示標題爲2012和2014的列。要執行此操做,咱們須要突出顯示此列中的標題以及全部後續單元格,包括總計。爲矩陣建立BeforePrint事件:ui

// List of selected columns
 private List<int> markedColumns;
// Counter for columns in the first row
 private int firstLineColumn;
 // Counter for columns in the following lines
 private int secondLineColumn;
 
 
// Matrix event handler
 private void Matrix2_BeforePrint(object sender, EventArgs e)
 {
// Create a new list for selected columns
 markedColumns = new List<int>();
// Reset the first row counter
 firstLineColumn = 0;
// Reset the next row count
 secondLineColumn = 0;
 }

最初,咱們添加了幾個咱們將在事件處理程序中使用的變量。這些變量存儲行的標記列。此外,在顯示矩陣以前,咱們初始化變量。this

爲值爲[Year]的單元格的BeforePrint事件建立另外一個處理程序:url

// Event handler for cells in the first row of the matrix
 private void Cell18_BeforePrint(object sender, EventArgs e)
 {
 // Use sender as TableCell
 TableCell cell = sender as TableCell;
 // Check the required value in the cell
 if (cell.Text == "2012" || cell.Text == "2014") 
 {
 // Sets the fill color for this cell.
 cell.FillColor = Color.Brown;
 // Save to selected list of columns
 markedColumns.Add(firstLineColumn);
 }
 // Increase column count for first row
 firstLineColumn++;
 }

在這裏,我須要作一個小的評論,以便你瞭解正在發生的事情的本質。關鍵是在FastReport中構建報表時的矩陣輸出是逐行完成的。所以,咱們保存矩陣第一行的列號。在咱們的例子中,2個值將落入標記列的列表中。.net

如今爲值爲[Revenue]的單元格添加事件處理程序:設計

// The event handler for the cells in the following rows of the matrix. You need to set it for the second row and the totals.
 private void Cell21_BeforePrint(object sender, EventArgs e)
 {
 // Use sender as TableCell
 TableCell cell = sender as TableCell;
 // Find the current index in the markedColumns list
 if (markedColumns.IndexOf(secondLineColumn) != -1)
 {
 // Sets the fill color for this cell.
 cell.FillColor = Color.Red;
 }
 // Increase counter
 secondLineColumn++;
 // Reset counter for next row
 if (secondLineColumn >= firstLineColumn)
 secondLineColumn = 0;

在此處理程序中,咱們找到與第一行中所選列對應的列,並將其單元格繪製爲紅色。到達最後一列後,重置下一行的變量。如您所知,在構建報表時,矩陣的第二行是動態的。這意味着它將顯示在源中的每行數據。所以,咱們須要檢查每一行併爲正確列中的單元格着色。code

腳本中給出的解決方案是不尋常的,但對於這種狀況惟一可能的解決方案,由於矩陣是動態構建的,而且不存儲單元格的最終結構,座標和位置,直到它直接在工做表上繪製。只有模板(咱們在設計器中看到的模板)和矩陣標題的文本值存儲在內存中。

所以,咱們必須遍歷全部行並記住列以進行着色。

根據腳本,咱們必須爲矩陣建立三個事件處理程序BeforePrint,單元格[Year]和單元格[Revenue]。可是,在咱們的矩陣中還有另外一個第三行。它顯示結果,根據所選列繪製它們也是很好的。爲此,對於位於[Revenue]下的單元格,只需從同一[Revenue]掛鉤BeforeFrint事件處理程序:

如今,運行報表:

若是要以不一樣的顏色繪製總計,則必須爲總計的單元格建立本身的BeforePrint事件處理程序,相似於[Revenue]單元格的處理程序。

相關文章
相關標籤/搜索