@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:
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:
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.