告生成器FastReport .NET功能指南:通过一维数组在多个页面上对类似矩阵进行排序

在FastReport .NET中,可以通过一维数组在多个页面上对类似矩阵进行排序。按照自己的意愿来操作矩阵中的数据,并在 告的不同页面上应用相同的排序顺序。

表生成器

假设我们有一个任务:按照所需的顺序对第一页上的矩阵进行排序,记住这个顺序并在其他页面上申请类似的矩阵。

当 告中有多个页面显示标题相同但包含不同数据的矩阵时,可能需要这样做。例如,第一个矩阵显示销售的产品数量,第二个矩阵显示按产品分类的销售额。我们需要按数量或金额排序,然后对第二个矩阵应用相同的顺序。这种情况在分析 告中很常见。

让我们在实践中看到它。我们采取一个完全假设的水果销售统计数据。但是,只有水果的种类是不够的,还会有水果进口国的名单。售出商品数量将显示三年。

 告生成器FastReport .NET功能指南:通过一维数组在多个页面上对类似矩阵进行排序

表结构:

  • 国家的名字
  • 水果类型
  • 年份
  • 数量
  • 价钱
  • 总和

排序

标准的排序机制在这里对我们没有帮助。因此,我们将对每个国家销售的水果数量进行排序。让我们概述一系列步骤:

  1.  获取国家列表。
  2.  每个国家:

2.1. 获取带有水果类型的单元格的值以及每年销售的产品数量;

2.2. 对所需年份的值进行排序;

2.3. 对于每一行,根据排序列表中行的索引填充水果的单元格和所有年份的数字。

第一列是国家,这对我们来说没问题,这意味着我们将对其余列的单元格进行排序。我们首先需要记住它们,以便我们可以根据排序计划将它们排列成所需的顺序。我们将选择包含特定年份数据的列之一,并按降序或升序对其进行排序。然后我们将使用生成的索引顺序按列对所有单元格进行排序。

矩阵有一个用于修改已构造对象的事件 – ModifyResult。让我们在 告脚本中为此事件创建一个处理程序。

 private List<List<int>> sortOrders = new List<List<int>>();//List of sorting orders for each collection of fruit species by country private void Matrix1_ModifyResult(object sender, EventArgs e) {//Dictionaries in which we will store the row index and cell value Dictionary<int, double> firstYearCells = new Dictionary<int, double>(); Dictionary<int, double> secondYearCells = new Dictionary<int, double>(); Dictionary<int, double> thirdYearCells = new Dictionary<int, double>(); Dictionary<int, string> typeCells = new Dictionary<int, string>(); Dictionary<int, double> sortCells = new Dictionary<int, double>();//bool prevYearSortNeeded = false; var total = false; var z = 1; var val2 = 0.0; var val3 = 0.0; List<string> countries = new List<string>(); //We will store the list of countries in this list //We get all countries from the first column for (int j=2; j<(sender as TableBase).ResultTable.RowCount-1; j++) { try { var val = (sender as TableBase).ResultTable.GetCellData(0,j).Value.ToString(); if (val.Length > 0) countries.Add(val); } catch (Exception) {} } int columnFirstYearIndex=0; int columnSecondYearIndex=0; int columnThirdYearIndex=0; int columnTypeIndex=0;//We go through all the columns of the matrix to save the cells in dictionaries for (int t=0; t < (sender as TableBase).ResultTable.ColumnCount; t++) { if ((sender as TableBase).ResultTable.GetCellData(t,0).Text.Contains("2017")) { columnFirstYearIndex=t; } if ((sender as TableBase).ResultTable.GetCellData(t,0).Text.Contains("2018")) { columnSecondYearIndex=t; } if ((sender as TableBase).ResultTable.GetCellData(t,0).Text.Contains("2019")) { columnThirdYearIndex=t; } if ((sender as TableBase).ResultTable.GetCellData(t,0).Text.Contains("Fruit")) { columnTypeIndex=t; } } int countryOrder =0; //We run a loop to identify the fruit groups and sort them for each country foreach (var country in countries) { total = false; sortCells.Clear(); //We clear the list for sorting //We select cells from rows until we see Total, since Total should not be sorted while (!total) { if ((string)(sender as TableBase).ResultTable.GetCellData(columnTypeIndex,z).Text!="Total") { //We select cells for the first year var value = (sender as TableBase).ResultTable.GetCellData(columnFirstYearIndex,z).Value; if (value!=null) { Double.TryParse(value.ToString(),out val3); firstYearCells.Add(z,val3); } else firstYearCells.Add(z, 0.0);//We select cells for the second year value = (sender as TableBase).ResultTable.GetCellData(columnSecondYearIndex,z).Value; if (value!=null) { Double.TryParse(value.ToString(),out val3); secondYearCells.Add(z,val3); } else secondYearCells.Add(z, 0.0); //We select cells for the third year value = (sender as TableBase).ResultTable.GetCellData(columnThirdYearIndex,z).Value; if (value!=null) { Double.TryParse(value.ToString(),out val3); thirdYearCells.Add(z,val3); } else thirdYearCells.Add(z, 0.0);//We select cells for fruit types value = (sender as TableBase).ResultTable.GetCellData(columnTypeIndex,z).Text; typeCells.Add(z,value.ToString()); } else {//Exit condition of the loop total = true; } z++; } sortCells = firstYearCells; //We set the column for sorting - in this case by the first year List<int> keys = new List<int>();//If we have a filled list of sorts for all countries, then the first page of the report has been built and you can use this list on the second page. This is where sorting through one-dimensional array is ensured. if ( sortOrders.Count == countries.Count ) { keys = sortOrders.ElementAt(countryOrder); } else keys = sortCells.OrderByDescending(i=>i.Value).Select(key => key.Key).ToList();//Sort the array in descending order using the Linq library int k = 0;//Loop through all the elements of the sorted list foreach(var key in keys) {//Build cell values for all columns in sort order (sender as TableBase).ResultTable.GetCellData(columnFirstYearIndex, firstYearCells.Keys.ElementAt(k)).Text = firstYearCells[key].ToString(); (sender as TableBase).ResultTable.GetCellData(columnSecondYearIndex, secondYearCells.Keys.ElementAt(k)).Text = secondYearCells[key].ToString(); (sender as TableBase).ResultTable.GetCellData(columnThirdYearIndex, thirdYearCells.Keys.ElementAt(k)).Text = thirdYearCells[key].ToString(); (sender as TableBase).ResultTable.GetCellData(columnTypeIndex, typeCells.Keys.ElementAt(k)).Text = typeCells[key].ToString(); k++; } if (keys.Count>0) sortOrders.Add(new List<int>(keys));//Save the sort order for the current country //It's important to clear firstYearCells.Clear(); secondYearCells.Clear(); thirdYearCells.Clear(); typeCells.Clear(); countryOrder++;//Go to the next country }}}

现在我们复制带有矩阵的 告页面,但我们将输出总和,而不是金额字段。

我们将选择我们在矩阵事件中为ModifyResult创建的处理程序。

 告生成器FastReport .NET功能指南:通过一维数组在多个页面上对类似矩阵进行排序

运行 告后,我们会看到两页上的水果类型的顺序是一样的。这意味着,第一页上的排序适用于第二页。

 告生成器FastReport .NET功能指南:通过一维数组在多个页面上对类似矩阵进行排序

因此,使用 告脚本,我们可以按照自己的意愿来操作矩阵中的数据。最重要的是,在 告的不同页面上应用相同的排序顺序。


Fastreport.NET在线购买价更低!赶紧加入购物清单吧!

如果您有任何疑问或需求,请随时加入FastReport技术交流群(702295239),我们很高兴为您提供查询和咨询。

标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年8月24日
下一篇 2021年8月24日

相关推荐

发表回复

登录后才能评论