Search code examples
c#consoledrawing

How To: Best way to draw table in console app (C#)


I have an interesting question. Imagine I have a lot of data changing in very fast intervals. I want to display that data as a table in console app. f.ex:

-------------------------------------------------------------------------
|    Column 1     |    Column 2     |    Column 3     |    Column 4     |
-------------------------------------------------------------------------
|                 |                 |                 |                 |
|                 |                 |                 |                 |
|                 |                 |                 |                 |
-------------------------------------------------------------------------

How to keep things fast and how to fix column widths ? I know how to do that in java, but I don't how it's done in C#.


Solution

  • You could do something like the following:

    static int tableWidth = 73;
    
    static void Main(string[] args)
    {
        Console.Clear();
        PrintLine();
        PrintRow("Column 1", "Column 2", "Column 3", "Column 4");
        PrintLine();
        PrintRow("", "", "", "");
        PrintRow("", "", "", "");
        PrintLine();
        Console.ReadLine();
    }
    
    static void PrintLine()
    {
        Console.WriteLine(new string('-', tableWidth));
    }
    
    static void PrintRow(params string[] columns)
    {
        int width = (tableWidth - columns.Length) / columns.Length;
        string row = "|";
    
        foreach (string column in columns)
        {
            row += AlignCentre(column, width) + "|";
        }
    
        Console.WriteLine(row);
    }
    
    static string AlignCentre(string text, int width)
    {
        text = text.Length > width ? text.Substring(0, width - 3) + "..." : text;
    
        if (string.IsNullOrEmpty(text))
        {
            return new string(' ', width);
        }
        else
        {
            return text.PadRight(width - (width - text.Length) / 2).PadLeft(width);
        }
    }