question

MarkusFreitag-0088 avatar image
0 Votes"
MarkusFreitag-0088 asked AgaveJoe edited

C# 2D array - input -> output problem

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


dotnet-csharpwindows-forms
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

What is the best way to detect this?

What exactly are you trying to detect???? Are you asking how to write code to open a file and split a string by a semicolon where the first line is a header?

 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");
             }
         }

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


1 Vote 1 ·

@MarkusFreitag-0088, Welcome to Microsoft Q&A, based on your description, I have some questions to confirm with you.

Input 09.05.2022 --> Output 214

First, It seems that you provided the wrong result about the output, because there is no data called 214 in the table.

Second, the picture you provided seems a csv file, but you provided a txt file. Do you want to read it from the txt file or the csv file?


1 Vote 1 ·

Thanks for the response.

200513--date-time-1.png




First I need to read the file as you show me.

Second I need a function

 string GetNewDateTime(DateTime dt);


 string ret = GetNewDateTime(DateTime.Now);


Third, do you see a way to create the table without a file?
I'm not sure which is better and easier. Could I also take a DataTable there?






0 Votes 0 ·
-date-time-1.png (21.2 KiB)
JackJJun-MSFT avatar image
1 Vote"
JackJJun-MSFT answered Viorel-1 commented

@MarkusFreitag-0088, 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.



image.png (24.2 KiB)
image.png (10.5 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Looks good, thanks.

You make also a user interface. DatagridControl? Can you publish it?

0 Votes 0 ·
JackJJun-MSFT avatar image JackJJun-MSFT MarkusFreitag-0088 ·

@MarkusFreitag-0088, I added a datagridview in winform and I have updated it in my answer.

1 Vote 1 ·

Thanks for your help!

0 Votes 0 ·
AgaveJoe avatar image
0 Votes"
AgaveJoe answered AgaveJoe edited

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




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.