// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Drawing; namespace Spludlow.Drawing { public class DrawTables { public static int Table(Spludlow.Printing.PrintDoc printDoc, DataTable table, float x, float y, float tableWidth, float tableHeight, float rowHeight, int startRowIndex) { TableStyle style = new TableStyle(); return Table(printDoc, table, x, y, tableWidth, tableHeight, rowHeight, startRowIndex, style); } public static int Table(Spludlow.Printing.PrintDoc printDoc, DataTable table, float x, float y, float tableWidth, float tableHeight, float rowHeight, int startRowIndex, TableStyle style) { string[] columnsInfo = null; if (columnsInfo == null) { columnsInfo = new string[table.Columns.Count]; for (int columnIndex = 0; columnIndex < table.Columns.Count; ++columnIndex) columnsInfo[columnIndex] = table.Columns[columnIndex].ColumnName; } // ColumnName width Heading format ?StringAlignment //columnsInfo = new string[] //{ // "OrderID 30 Order No", // "CustomerID 30 Customer Ref", // "OrderDate 30 Order Date {0:d}", // "Freight 30 Freight {0:C}", // "ShipAddress 80 Ship Address", //}; DataTable defTable = Spludlow.Data.TextTable.ReadText(new string[] { "ColumnName Width Heading Format Position", "String Single String String Single", }); float defaultColumnWidth = tableWidth / columnsInfo.Length; // Must have tableWidth float position = 0; foreach (string line in columnsInfo) { string[] words = Spludlow.Text.Split(line, '\t', true, false); string columnName = ""; if (words.Length > 0) columnName = words[0]; float columnWidth = defaultColumnWidth; if (words.Length > 1) columnWidth = Single.Parse(words[1]); string heading = ""; if (words.Length > 2) heading = words[2]; string format = ""; if (words.Length > 3) format = words[3]; defTable.Rows.Add(columnName, columnWidth, heading, format, position); position += columnWidth; } position = y; if (style.HeadingBackColour != Color.Empty) printDoc.Rectangle(x, y, tableWidth, rowHeight, 0, Color.Empty, style.HeadingBackColour); foreach (DataRow defRow in defTable.Rows) { string columnName = (string)defRow["ColumnName"]; if (columnName == "") continue; string heading = (string)defRow["Heading"]; if (heading == "") heading = columnName; float width = (float)defRow["Width"]; float xPosition = (float)defRow["Position"]; printDoc.TextBox(heading, style.HeadingFont, x + xPosition, position, width, rowHeight, style.HeadingForeColour, StringAlignment.Center); } position += rowHeight; int index; for (index = startRowIndex; index < table.Rows.Count && (position <= (y + tableHeight)); ++index) { DataRow row = table.Rows[index]; foreach (DataRow defRow in defTable.Rows) { string columnName = (string)defRow["ColumnName"]; if (columnName == "") continue; if (row.IsNull(columnName) == true) continue; float columnWidth = (float)defRow["Width"]; string format = (string)defRow["Format"]; float xPosition = (float)defRow["Position"]; Color rowUseBackColour = style.RowBackColour; if ((index % 2) != 0) rowUseBackColour = style.RowBackAltColour; if (rowUseBackColour != Color.Empty) printDoc.Rectangle(x + xPosition, position, columnWidth, rowHeight, 0, Color.Empty, rowUseBackColour); Color rowUseForeColour = style.RowForeColour; if ((index % 2) != 0) rowUseForeColour = style.RowForeAltColour; string text; if (format == "") text = Convert.ToString(row[columnName]); else text = String.Format(format, row[columnName]); printDoc.TextBox(text, style.RowFont, x + xPosition, position, columnWidth, rowHeight, rowUseForeColour); } if (style.HorizontalLineWeight > 0) printDoc.Line(x, position, x + tableWidth, position, style.HorizontalLineWeight, style.LineColour); position += rowHeight; } if (style.OuterLineWeight > 0) printDoc.Rectangle(x, y, tableWidth, position - y, style.OuterLineWeight, style.LineColour, Color.Empty); if (style.VerticalLineWeight > 0) { foreach (DataRow defRow in defTable.Rows) { float width = (float)defRow["Width"]; float xPosition = (float)defRow["Position"]; if ((x + xPosition + width) != (x + tableWidth)) printDoc.Line(x + xPosition + width, y, x + xPosition + width, position, style.VerticalLineWeight, style.LineColour); } } if (index >= (table.Rows.Count - 1)) index = (int)-position; return index; } } }