0

I have data that is stored in a 2-Dimensional List that I want to print to the console window where it is all lined up properly.

Example:

Dim aList As New List Of(List Of String))
aList = AfunctionThatFetchesData

aList

 {column 1}         {column 2} {column 3}
 This is some data   0          3
 Some more           1          3
 One more            2          3
user3586138
  • 13
  • 1
  • 3
  • possible duplicate of [How to print List as table in console application?](http://stackoverflow.com/questions/10160770/how-to-print-list-as-table-in-console-application) – mr_plum Apr 29 '14 at 18:11

2 Answers2

4

Check out the documentation for Console.WriteLine, where you will see that it uses the composite formatting feature, which support alignment parameters. So, you can align things using, e.g.

Console.WriteLine("{0,-20} {1,-10} {2,-10}", "{column 1}", "{column 2}", "{column 3}")
Console.WriteLine("{0,-20} {1,-10} {2,-10}", "This is some data", 0, 3)

which results in:

{column 1}           {column 2} {column 3}
This is some data    0          3

Adjusting the spacing and alignments in the format string will get you what you want.

Mark
  • 8,140
  • 1
  • 14
  • 29
1

If you want the user to be able to manually enter data into a table:

Console.Write("Enter Data For Column 1: ")
    Dim Data1 As String = Console.ReadLine
    Console.Write("Enter Data For Column 2: ")
    Dim Data2 As String = Console.ReadLine

    Console.WriteLine("{0,-20} {1,-10} {2,-10}", "{Data Type}", "{Column 1}", "{Column 2}")
    Console.WriteLine("{0,-20} {1,-10} {2,-10}", "Data Entered:", Data1, Data2)

    Console.WriteLine("ENTER To Exit: ")
    Console.ReadLine()

It should look like this (Click Me).

BritNinjah
  • 15
  • 3