using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PrintRowsAndColumns
{
    public partial class frm94PrintRowsAndColumns : Form
    {
        public frm94PrintRowsAndColumns()
        {
            InitializeComponent();
        }

        // The data to print.
        private string[] Headers = { "Title", "URL", "Pages", "Year" };
        private string[,] Data =
        {
            {"Interview Puzzles Dissected", "tinyurl.com/j3nex2u", "300", "2016"},
            {"C# 24-Hour Trainer, 2nd Edition", "tinyurl.com/n2a5797", "600", "2015"},
            {"Beginning Software Engineering", "tinyurl.com/pz7bavo", "480", "2015"},
            {"C# 5.0 Programmer's Reference", "tinyurl.com/qzcefsp", "960", "2014"},
            {"Essential Algorithms", "tinyurl.com/ksrp6ot", "624", "2013"},
        };

        // Display a print preview.
        private void previewButton_Click(object sender, EventArgs e)
        {
            gridPrintPreviewDialog.ShowDialog();
        }

        // Print the document's page. (This version doesn't handle multiple pages.)
        private void gridPrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            // Use this font.
            using (Font headerFont = new Font("Times New Roman", 16, FontStyle.Bold))
            {
                using (Font bodyFont = new Font("Times New Roman", 12))
                {
                    // We'll skip this much space between rows.
                    int lineSpacing = 20;

                    // See how wide the columns must be.
                    int[] columnWidths = FindColumnWidths(
                        e.Graphics, headerFont, bodyFont, Headers, Data);

                    // Print the headers.
                    int x = e.MarginBounds.Left;
                    int y = e.MarginBounds.Top;
                    for (int col=0; col<Headers.Length; col++)
                    {
                        e.Graphics.DrawString(Headers[col],
                            headerFont, Brushes.Blue, x, y);
                        x += columnWidths[col];
                    }

                    // Move down a bit extra before the data.
                    y += (int)(lineSpacing * 1.5);

                    // Print by rows.
                    for (int row = 0; row <= Data.GetUpperBound(0); row++)
                    {
                        // Print this row's values.
                        x = e.MarginBounds.Left;
                        for (int col = 0; col < Headers.Length; col++)
                        {
                            e.Graphics.DrawString(Data[row, col],
                                bodyFont, Brushes.Black, x, y);
                            x += columnWidths[col];
                        }

                        // Move down a row.
                        y += lineSpacing;
                    }
                }
            }
            e.HasMorePages = false;
        }

        // Figure out how wide each column should be.
        private int[] FindColumnWidths(Graphics gr, Font headerFont, Font bodyFont, string[] headers, string[,] values)
        {
            // Make room for the widths.
            int[] widths = new int[headers.Length];

            // Find the width for each column.
            for (int col = 0; col < widths.Length; col++)
            {
                // Check the column header.
                widths[col] = (int)gr.MeasureString(headers[col], headerFont).Width;

                // Check the items.
                for (int row = 0; row <= values.GetUpperBound(0); row++)
                {
                    int valueWidth = (int)gr.MeasureString(values[row, col], bodyFont).Width;
                    if (widths[col] < valueWidth) widths[col] = valueWidth;
                }

                // Add some extra space.
                widths[col] += 20;
            }

            return widths;
        }


        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frm94PrintRowsAndColumns));
            this.previewButton = new System.Windows.Forms.Button();
            this.gridPrintDocument = new System.Drawing.Printing.PrintDocument();
            this.gridPrintPreviewDialog = new System.Windows.Forms.PrintPreviewDialog();
            this.SuspendLayout();
            // 
            // previewButton
            // 
            this.previewButton.Anchor = System.Windows.Forms.AnchorStyles.None;
            this.previewButton.Location = new System.Drawing.Point(116, 44);
            this.previewButton.Name = "previewButton";
            this.previewButton.Size = new System.Drawing.Size(102, 23);
            this.previewButton.TabIndex = 1;
            this.previewButton.Text = "Print Preview";
            this.previewButton.UseVisualStyleBackColor = true;
            this.previewButton.Click += new System.EventHandler(this.previewButton_Click);
            // 
            // gridPrintDocument
            // 
            this.gridPrintDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.gridPrintDocument_PrintPage);
            // 
            // gridPrintPreviewDialog
            // 
            this.gridPrintPreviewDialog.AutoScrollMargin = new System.Drawing.Size(0, 0);
            this.gridPrintPreviewDialog.AutoScrollMinSize = new System.Drawing.Size(0, 0);
            this.gridPrintPreviewDialog.ClientSize = new System.Drawing.Size(400, 300);
            this.gridPrintPreviewDialog.Document = this.gridPrintDocument;
            this.gridPrintPreviewDialog.Enabled = true;
            this.gridPrintPreviewDialog.Icon = ((System.Drawing.Icon)(resources.GetObject("gridPrintPreviewDialog.Icon")));
            this.gridPrintPreviewDialog.Name = "ppdGrid";
            this.gridPrintPreviewDialog.Visible = false;
            // 
            // frm94PrintRowsAndColumns
            // 
            this.AcceptButton = this.previewButton;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(334, 111);
            this.Controls.Add(this.previewButton);
            this.Name = "frm94PrintRowsAndColumns";
            this.Text = "PrintRowsAndColumns";
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button previewButton;
        internal System.Drawing.Printing.PrintDocument gridPrintDocument;
        internal System.Windows.Forms.PrintPreviewDialog gridPrintPreviewDialog;
    }
}