C# 2D array - input -> output problem

Markus Freitag 3,786 Reputation points
2022-05-09T15:00:40.06+00:00

Hello,

I have the following table.

Input 09.05.2022 --> Output 214
Input 09.08.2023 --> Output 344

What is the best way to detect this?
a) only code
b) read a file

Thanks for tips in advance.
200364--date-time-5.png

200318-datetime-csv.txt

;1;2;3;4;5;6;7;8;9;10;11;12  
2022;237;238;239;240;241;242;243;244;245;246;247;248  
2023;337;338;339;340;341;342;343;344;345;346;347;348  
2024;437;438;439;440;441;442;443;444;445;446;447;448  
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,871 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,633 questions
{count} votes

Accepted answer
  1. Jack J Jun 24,496 Reputation points Microsoft Vendor
    2022-05-10T07:05:03.847+00:00

    @Markus Freitag , you could try the following code to convert the file to a datatable and get the correspond value based on the input.

    Code example:

     static void Main(string[] args)  
            {  
                List<List<string>> table = new List<List<string>>();  
      
                foreach (string line in System.IO.File.ReadLines("datetime-csv.txt"))  
                {  
                    table.Add(line.Split(';').ToList());  
                }  
                DataTable table1 = new DataTable();  
                for (int i = 0; i < table[0].Count; i++)  
                {  
                    table1.Columns.Add("Col"+i);  
                }  
                foreach (var item in table)  
                {  
                    table1.Rows.Add(item.ToArray());  
                }  
      
                string ret = GetNewDateTime(DateTime.Now);  
                var year = Convert.ToInt32(ret.Substring(0, 4));  
                var month = Convert.ToInt32(ret.Substring(4));  
                string pattern = string.Format("Col0='{0}'", year);  
                DataRow[] result = table1.Select(pattern);  
                int rowindex = table1.Rows.IndexOf(result[0]);  
                string result1 = table[rowindex][month].ToString();  
                Console.WriteLine(result1);  
                
                Console.ReadLine();  
      
            }  
      
            static string GetNewDateTime(DateTime dt)  
            {  
                return dt.ToString("yyyyMM");  
            }  
    

    You could get the result 241 and datatable:

    200562-image.png

    Update for show it in a datagirdview:

      private void Form1_Load(object sender, EventArgs e)  
            {  
                List<List<string>> table = new List<List<string>>();  
      
                foreach (string line in System.IO.File.ReadLines("datetime-csv.txt"))  
                {  
                    table.Add(line.Split(';').ToList());  
                }  
                DataTable table1 = new DataTable();  
                for (int i = 0; i < table[0].Count; i++)  
                {  
                    table1.Columns.Add("Col" + i);  
                }  
                foreach (var item in table)  
                {  
                    table1.Rows.Add(item.ToArray());  
                }  
                dataGridView1.DataSource= table1;  
            }  
    

    Result:

    200612-image.png

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. AgaveJoe 27,656 Reputation points
    2022-05-10T19:41:19.567+00:00

    Your posts are always very confusing... I think you are asking how to use an index to return a value from a multidimensional array. In my opinion the data has unnecessary header which I assume are months. Anyway, indexes are needed to find the data. I used an enum but there are other ways to do this. You just need to a way to map the indexes to the rows and columns.

        public enum Year
        {
            _2022 = 1,
            _2023 = 2,
            _2024 = 3
        }
    
        public enum Month
        {
            Jan = 1,
            Feb = 2,
            Mar = 3,
            Apr = 4,
            May = 5,
            Jun = 6,
            Jul = 7,
            Aug = 8,
            Sep = 9,
            Oct = 10,
            Nov = 11,
            Dec = 12
        }
    
        class Program
        {
            static async Task Main(string[] args)
            {
                List<List<string>> table = new List<List<string>>();
    
                foreach (string line in System.IO.File.ReadLines("datetime-csv.txt"))
                {
                    table.Add(line.Split(";").ToList());
                }
    
                foreach(List<string> row in table)
                {
                    foreach(string cell in row)
                    {
                        Console.Write($"{cell}\t");
                    }
                    Console.Write("\r\n");
                }
    
                Console.WriteLine();
                string result = GetValueByYearAndMonth(Year._2022, Month.May, table);
                Console.WriteLine($"Value = {result}");
    
                Console.WriteLine();
                string result2 = GetValueByYearAndMonth2(DateTime.Now, table);
                Console.WriteLine($"Value = {result2}");
            }
    
            private static string GetValueByYearAndMonth(Year year, Month month, List<List<string>> data)
            {
                return data[(int)year][(int)month];
            }
    
            private static string GetValueByYearAndMonth2(DateTime dt, List<List<string>> data)
            {
                int row = dt.Year - 2021;
                int month = dt.Month;
                return data[row][month];
            }
        }
    

    Output

            1       2       3       4       5       6       7       8       9       10      11      12
     2022   237     238     239     240     241     242     243     244     245     246     247     248
     2023   337     338     339     340     341     342     343     344     345     346     347     348
     2024   437     438     439     440     441     442     443     444     445     446     447     448
    
    Value = 241
    
    Value = 241
    
    0 comments No comments