0

I am new at programming in C# and I'm trying to display to whole content of a matrix in the format of a table, however what I got so far is to read the enum and read one line from the matrix. Instead I need to read multiple lines from the matrix and output as a table.

when I run the program I get to insert data twice in row and the output should've been this data inside a table, but only one line is being shown. Here's the code:

static int getInsertIndex(string[,] matrix)
        {
            for (int j = 0; j < matrix.GetLength(0); j++)
            {
                if (string.IsNullOrEmpty(matrix[j, 0])) return j;
            }

            return -1;
        }
        private static void InsertData<T>(string[,] matrix)
        {

            // int newId = generateId(ref id);
            int n = getInsertIndex(matrix), id = 1;

            matrix[n, 0] = Convert.ToString(id++);
            int x = matrix.GetLength(1) - 1;
            matrix[n, x] = "true";

            for (var j = 1; j < matrix.GetLength(1); j++)
            {
                do
                {
                    Console.Write($"\nInsert {GetHeader<T>(j)}: ");
                    matrix[0, j] = Console.ReadLine();
                } while (string.IsNullOrEmpty(matrix[0, j]));
            }
        }

        private static void ListData<T>(string[,] matrix)
        {
            var array = new string[matrix.GetUpperBound(1)];

            for (var l = 0; l < matrix.GetLength(0); l++)
            {
                for (var i = 0; i < array.Length; i++)
                {
                    array[i] = matrix[0, i];
                }
            }

            PrintRow(array);
            PrintLine();
        }

        private static string GetHeader<T>(int i) => Enum.GetName(typeof(T), i);

        private static void ShowHeader<T>(string[,] matrix)
        {
            var array = new string[matrix.GetUpperBound(1)];

            for (var i = 0; i < array.Length; i++)
            {
                array[i] = GetHeader<T>(i);
            }

            PrintLine();
            PrintRow(array);
            PrintLine();
        }

        private static void PrintLine()
        {
            Console.WriteLine(new string('-', Console.WindowWidth - 1));
        }

        private static void PrintRow(IReadOnlyCollection<string> columns)
        {
            var width = (Console.WindowWidth - 1 - columns.Count) / columns.Count;
            var row = columns.Aggregate("|", (current, column) => current + AlignCentre(column, width) + "|");
            Console.WriteLine(row);
        }

        static string AlignCentre(string text, int width)
        {
            text = text.Length > width ? text.Substring(0, width - 3) + "..." : text;

            return string.IsNullOrEmpty(text)
                ? new string(' ', width)
                : text.PadRight(width - (width - text.Length) / 2).PadLeft(width);
        }

        enum ClientHeader { Id, Name, Surname, Addres, CodPostal, Telephone, Email, State };

        private static void Main()
        {
            var client = new string[4, 7];
            InsertData<ClientHeader>(client);

            Console.Clear();
            InsertData<ClientHeader>(client);

            ShowHeader<ClientHeader>(client);
            ListData<ClientHeader>(client);

        }
    }
}

2 Answers 2

1

There are 2 issues:

1) In the InsertData() function, you want to update the n th row.

Replace

matrix[0, j] = Console.ReadLine();

by

matrix[n, j] = Console.ReadLine();

2) In the ListData() function you want to show each row, so you need to move the array variable into the first for loop. Replace array[i] = matrix[0, i] with array[i] = matrix[l, i] because you are displaying the l th row.

private static void ListData<T>(string[,] matrix)
{
    for (var l = 0; l < matrix.GetLength(0); l++)
    {
        var array = new string[matrix.GetUpperBound(1)];

        for (var i = 0; i < array.Length; i++)
        {
            array[i] = matrix[l, i];
        }

        PrintRow(array);
    }

    PrintLine();
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try following :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;


namespace ConsoleApplication1
{
    public class Program
    {
        private static void Main()
        {
            List<List<string>> data = new List<List<string>>() {
                new List<string>() { "Name", "Age", "Weight"},
                new List<string>() { "John", "33", "180"},
                new List<string>() { "Mary", "32", "125"},
                new List<string>() { "Harry", "40", "200"}
            };

            DataTable dt = new DataTable();
            for (int i = 0; i < data.Count; i++)
            {
                if (i == 0)
                {
                    foreach (string col in data[i])
                    {
                        dt.Columns.Add(col);
                    }
                }
                else
                {
                    dt.Rows.Add(data[i].ToArray());
                }
            }

        }
    }

}

3 Comments

is there a way without DataTable??
You request was for a table. What other type table do you mean.
the output has to be a table but I was trying to do from an array 2 D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.