Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print List as table in console application? [duplicate]

Tags:

I need Method to print List as table in console application and preview in convenient format like this:

Pom_No          Item_Code          ordered_qty                      received_qty  1011            Item_Code1         ordered_qty1                    received_qty1   1011            Item_Code2         ordered_qty2                    received_qty2  1011            Item_Code3         ordered_qty3                    received_qty3  1012            Item_Code1         ordered_qty1                    received_qty1   1012            Item_Code2         ordered_qty2                    received_qty2  1012            Item_Code3         ordered_qty3                    received_qty3 
like image 830
Anas Jaber Avatar asked Apr 15 '12 09:04

Anas Jaber


People also ask

How to Print output in table format in c#?

Print DataTable , DataView , DataSet , DataRow[] (= DataTable. Select() ) Print Columns. Print in tabular format and list format.

How do I make a table in console?

Open Chrome developer tools (press F12). Then create an array of hashes of data. The hash keys will be used as table columns, and the values will be used as table rows. Apparently console.


1 Answers

Your main tool would be

Console.WriteLine("{0,5} {1,10} {2,-10}", s1, s2, s3);   

The ,5 and ,10 are width specifiers. Use a negative value to left-align.

Formatting is also possible:

Console.WriteLine("y = {0,12:#,##0.00}", y); 

Or a Date with a width of 24 and custom formatting:

String.Format("Now = {0,24:dd HH:mm:ss}", DateTime.Now); 

Edit, for C#6

With string interpolation you can now write

Console.WriteLine($"{s1,5} {s2,10} {s3,-10}");   Console.WriteLine($"y = {y,12:#,##0.00}"); 

You don't need to call String.Format() explicitly anymore:

string s = $"Now = {DateTime.Now,24:dd HH:mm:ss}" + ", " + $"y = {y,12:#,##0.00}" ; 
like image 168
Henk Holterman Avatar answered Sep 30 '22 16:09

Henk Holterman