C# Code Snippets

Print Console Table

Max Black

This Class allows any Console app to create a basic table. The columns are auto sized to the longest cell. Every Row must have the same amount of cells. Note: The class can display as many rows as you want, but a limitation I saw, was the console would cut off everything past about 35 entries. I tried to fix it by changing the Console.BufferHeight, but it didn't work. Any suggestions would be great!

A great sample data set can be gotten from: http://www.vcskicks.com/code-snippet/PeriodicTableInArray.php Base of this code was taken from: http://stackoverflow.com/questions/856845/how-to-best-way-to-draw-table-in-console-app-c by Patrick McDonald

Platform: .NET Framework 2.0

Usage:
string[] headers = new string[] {"Atomic Number", "Abreviation", "Name", "Atomic Weight"};
UI_Format ConsoleFormatter = new UI_Format(3, UI_Format.Align.Left, headers);
ConsoleFormatter.RePrint(data); //Get data variable from the link in the description

Class:
    class UI_Format
    {
        public enum Align {Left, Right};
        private string[] headers;
        private Align CellAlignment = Align.Left;
        private int tableYStart = 0;
        /// <summary>
        /// The last line of the table (gotton from Console.CursorTop). -1 = No printed data
        /// </summary>
        public int LastPrintEnd = -1;

        /// <summary>
        /// Helps create a table
        /// </summary>
        /// <param name="TableStart">What line to start the table on.</param>
        /// <param name="Alignment">The alignment of each cell\'s text.</param>
        public UI_Format(int TableStart, Align Alignment, string[] headersi)
        {
            headers = headersi;
            CellAlignment = Alignment;
            tableYStart = TableStart;
        }
        public void ClearData()
        {
            //Clear Previous data
            if (LastPrintEnd != -1) //A set of data has already been printed
            {
                for (int i = tableYStart; i < LastPrintEnd; i++)
                {
                    ClearLine(i);
                }
            }
            LastPrintEnd = -1;
        }
        public void RePrint(ArrayList data)
        {
            //Set buffers
            if (data.Count > Console.BufferHeight)
                Console.BufferHeight = data.Count;
            //Clear Previous data
            ClearData();

            Console.CursorTop = tableYStart;
            Console.CursorLeft = 0;
            if (data.Count == 0)
            {
                Console.WriteLine("No Records");
                LastPrintEnd = Console.CursorTop;
                return;
            }
            
            //Get max lengths on each column
            int ComWidth = ((string[])data[0]).Length * 2 + 1;
            int[] ColumnLengths = new int[((string[])data[0]).Length];

            foreach (string[] row in data)
            {
                for (int i = 0; i < row.Length; i++)
                {
                    if (row[i].Length > ColumnLengths[i])
                    {
                        ComWidth -= ColumnLengths[i];
                        ColumnLengths[i] = row[i].Length;
                        ComWidth += ColumnLengths[i];
                    }
                }
            }
            //Don't forget to check headers
            for (int i = 0; i < headers.Length; i++)
            {
                if (headers[i].Length > ColumnLengths[i])
                {
                    ComWidth -= ColumnLengths[i];
                    ColumnLengths[i] = headers[i].Length;
                    ComWidth += ColumnLengths[i];
                }
            }


            if (Console.BufferWidth < ComWidth)
                Console.BufferWidth = ComWidth + 1;
            PrintLine(ComWidth);
            //Print Data
            bool first = true;
            foreach (string[] row in data)
            {
                if (first)
                {
                    //Print Header
                    PrintRow(headers, ColumnLengths);
                    PrintLine(ComWidth);
                    first = false;
                }
                PrintRow(row,ColumnLengths);
            }
            PrintLine(ComWidth);
            LastPrintEnd = Console.CursorTop;
        }

        private void ClearLine(int line)
        {
            int oldtop = Console.CursorTop;
            Console.CursorTop = line;
            int oldleft = Console.CursorLeft;
            Console.CursorLeft = 0;
            int top = Console.CursorTop;

            while (Console.CursorTop == top)
            { Console.Write(" "); }
            Console.CursorLeft = oldleft;
            Console.CursorTop = oldtop;
        }

        private void PrintLine(int width)
        {
            Console.WriteLine(new string('-', width));
        }

        private void PrintRow(string[] row, int[] Widths)
        {
            string s = "|";
            for (int i = 0; i < row.Length; i++)
            {
                if (CellAlignment == Align.Left)
                    s += row[i] + new string(' ', Widths[i] - row[i].Length + 1) + "|";
                else if (CellAlignment == Align.Right)
                    s += new string(' ', Widths[i] - row[i].Length + 1) + row[i] + "|";
            }
            if (s == "|")
                throw new Exception("PrintRow input must not be empty");

            Console.WriteLine(s);
        }
    }

 

Back to C# Code Snippet List

Other C# Articles